Counting words entered in a text box by a visitor and how to limit such entries using JavaScript code for the HTML TEXTAREA element
counting words entered in a text box, limiting input from text box textarea element, word count in textarea element of HTML forms, javascript code and function to count words entered in a textarea element
Counting words entered in a text box Go to Counting words entered in a text box HTML & JavaScriptGo to HTML and JavaScript tips and tricksTips and tricksGo to web development Tips and tricksHomepage

Counting words entered in a text box

Here is a JavaScript function that counts the number of words typed in a text box. In the next tip we'll see how to put this function to some good use. The function can be called from the onclick() event of a submit button.

function count_words(tbox_input)
   {
   no_words = tbox_input.value.split(" ");
   alert("Text box has " + no_words.length + " words");
   return false;
   }

The reference to the TEXTAREA is passed as an argument to the function. We then employ the split() method with the space character to split the value of TEXTAREA. The result is an array that is stored in no_words variable. The number of words can now be calculated with the length property of the array data type.

The code for the HTML form is as follows:

<FORM NAME="myform">
<TEXTAREA NAME="details" COLS="30" ROWS="6">
This text box has six words.
</TEXTAREA>
<INPUT TYPE="SUBMIT" VALUE="Check" 
ONCLICK="return count_words(document.myform.details)">
</FORM>

Note: The text placed between <TEXTAREA> - </TEXTAREA> tags is the value of the text box.


AddThis Social Bookmark Button


Page contents: JavaScript code in a function that helps in counting words entered in a form text box or an HTML TEXTAREA element and limiting visitor inputs via such text boxes.

Page URL: http://www.webdevelopersnotes.com/tips/html/ counting_words_entered_in_a_text_box_.php3



Join Mailing List


Feedback/Questions




50+ web hosting FAQs

Search engine for your website

Free software from Google - The Google Pack Collection

Create your own search engine
Search WebDevelopersNotes.com