Counting words entered in a text box
Sponsored Links
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.
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.
Comments, questions, feedback... whatever!
Recent Articles
Recent Blog Posts
Popular Articles
- Hotmail Sign In page
- Create a Hotmail account - Instructions
- Create Gmail address
- Download and install Outlook Express
- Get your free Gmail address
- Outlook Express new version
- Create my own email address
- Browers for Windows list
- Get email address
- Color combinations for web sites and pages
- Create Yahoo ID
More web tips & tricks
- Outlook Express Help - tips & tricks (53)
- Windows Live Mail help and tips (36)
- Windows Mail help (25)
- Hotmail help and tips (42)
- Yahoo help & support (45)
- AOL email help (26)
- Gmail help and tips (36)
- Internet tips & tricks (12)
- Web Design tips & tricks (33)
- HTML, Javascript tips tricks (68)
- Web Promotion tips & tricks (8)
- Graphics tips & tricks (20)
