Matt Alcock
Matt Alcock

Reputation: 12901

Fixed character limit text box with running feedback - javascript

Is there a simple javascript pattern for showing how many characters left you can use in a fixed character text box?

Say you have a text box and you want them only to use 500 characters, twitter and stack-overflow show how many you have left as you type. I presume you can use jquery and a type event to produce a dynamic running count.

Is there an elegant pattern for this in javascript.

Upvotes: 0

Views: 749

Answers (1)

arunes
arunes

Reputation: 3524

Yes there is tons of scripts out there. One of them is

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}
</script>

Script taken from: http://www.mediacollege.com/internet/javascript/form/limit-characters.html

Upvotes: 2

Related Questions