JavaScript Code

Count the number of words with JavaScript

Sometimes you need a quick way to count the number of words in a string. Suppose you've typed out a blog post or essay and you want to ensure the minimum word count. Well, you could past into Word or OpenOffice, but this is much quicker! This handy little code snippet provides a textarea and the inline JavaScript to display the number of words as you type.


<form name="form_count">
<textarea rows="8" name="word_input" cols="40" wrap="virtual" onkeyup="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;"></textarea>
<p><input type="button" value="Count Words" onClick="f=document.form_count;f.word_count.value=f.word_input.value.split(' ').length;"> 
<input type="text" name="word_count" size="20"></p>
</form>

Here's what it looks like:


Share

6 Comments

  1. hey explain this to me, how do i set this up on a website?

  2. well ..this works fine only if i give spaces between two words but doesn't work when two words are comma separated..

  3. then just make the delimiter the comma character instead of a space

  4. This code is not able to calculate properly if words are seperated by more than one space

  5. I have been trying and trying to find a code like this that sets a minimum number of words required before the user can submit a form. Can you help with this?

Leave a Reply

Your email address will not be published. Required fields are marked *

*
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.
Anti-spam image