No account yet?

Links

Home arrow Articles arrow Programming arrow How to limit the number of words typed in a textbox using javascript
How to limit the number of words typed in a textbox using javascript PDF Print E-mail
Tag it:
Delicious
blogmarks
Stumble
Furl it!
Digg
YahooMyWeb
Technorati
User Rating: / 1
PoorBest 

In this example we are going to create a function that will check the length of a html textarea element and dynamically update two span tags.
The tags will display the number of words remaining and the number of words type. Note that this function will not count the characters typed, but only the words.  If the length exceeds the specified limit, then a message box will be displayed, and no furthur text can be entered in the textarea.

Here is the javascript function that we will call on to check the number of words.

 Here is what the textarea and the message span (s) look like.

<textarea name="textBox1" onkeypress=" return check_length(this, document.getElementById('count_number_words'), document.getElementById('show_remaining_words'));"></textarea>
<br>
<font color="black">Word count:</font><font color="red">
   <span id="count_number_words">0</span>
</font>
<br>
<font color="black">Words remaining: </font><font color="red">
   <span id="show_remaining_words">120</span>
</font>

Comments
Search
Only registered users can write comments!
Last Updated ( Wednesday, 25 June 2008 )
 
< Prev