September 11th, 2006

Count the Number of Characters with JavaScript

Sometimes you need a quick way to count the number of characters in a string. Suppose you're limiting a textarea to 1000 characters, you'll want to show the user how much more they can type. Or, perhaps a forum limits your signature length, and you want a quick way to validate it (including BB Code markup). This handy little code snippet provides a textarea and the inline JavaScript to display the number of characters.


<form name="form_count">
<textarea rows="8" name="char_input" cols="40" wrap="virtual" onkeyup="f=document.form_count;f.char_count.value=f.char_input.value.length;"></textarea>
<p><input type="button" value="Calculate Characters" onClick="f=document.form_count;f.char_count.value=f.char_input.value.length;">
<input type="text" name="char_count" size="20"></p>
</form>

Here's what it looks like:

Share

4 Responses to ' Count the Number of Characters with JavaScript '

Subscribe to comments with RSS or TrackBack to ' Count the Number of Characters with JavaScript '.

  1. kavitha said,

    on April 14th, 2008 at 5:44 am

    Thanks for the Javascript… i was searching for this type of Dynamically displaying script from a very long time…

    Thanks once again :)

  2. chris said,

    on July 16th, 2009 at 4:22 pm

    Many thanks for the script…very compact. It will be useful my site

  3. Kevlyn said,

    on July 18th, 2011 at 12:31 am

    Oh Very nice script, actually I got confused first, then I found this is a very good and small script. Thanks, Good Work. :-)

  4. dara said,

    on November 14th, 2011 at 7:49 am

    I want to test this code. So I want to see the process.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word