Javascript 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

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>

<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>
 

If you find this site useful and are a book buyer/reader...

Abe Books is one of my affiliates. I only use affiliates that I also purchase products from. I love Abe books. I have purchased many books from them originally cost $40 to $60 for as little as $5 to $10 dollars including shipping costs.

By accessing Abe books and clicking on one of my links I receive a small commission on your purchase that helps pay for this site. You pay no more for your books than if you accessed their site directly. Thanks for your support.

I have found Javascript - The Good Parts to be an indespensible resource

Click on the book to find books on Javascript at Abe Books...

If you would like a new version of the book click below to buy it from Amazon (also one of my affiliates).

Javascript Examples

Basics

Forms

Misc













navTango.com free

75% of your donation

goes to charity.