You are here: irt.org | FAQ | JavaScript | Form | Q1621 [ previous next ]
Try:
<script language="JavaScript"><!--
function wordcount(string) {
var a = string.split(/\s+/g); // split the sentence into an array of words
return a.length;
}
//--></script>
<form name="myForm">
<textarea name="myText" rows="3" cols="40">
This is some sample text. There is punctuation (sprinkled)
about, plus some line-separators. The answer should be 19.
</textarea>
<input type="button" value="Word Count" onClick="alert(wordcount(this.form.myText.value))">
</form>