In Action

Javascript example providing a running count of characters

This script provides a running total of characters entered into a text box or textarea.

Javascript in Action


( You may enter up to 125 characters. )

characters left



Javascript Code

Javascript Code

Just cut-n-paste to reuse.

    
<script language="JavaScript">
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
</script>

                                                                                                                                       

HTML Code

HTML Code


<center>
<form name=myform action="#">
<font size="1" face="arial, helvetica, sans-serif"> 
( You may enter up to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4 
 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" 
 onKeyUp="textCounter(this.form.message,this.form.remLen,125);"
    ></textarea>
<br>
<input readonly type=text name=remLen 
size=3 maxlength=3 value="125"> 
characters left</font>
</form>
</center>