Count Characters and Words

[Total: 1    Average: 3/5]

This example counts the number of words and characters in the form textarea.

Head Code:

<SCRIPT LANGUAGE="javascript">
<!--

function words(content){
	var i=0;
	var numberofwords=1;

	while(i<=content.length) {

		if (content.substring(i,i+1) == " ") {
			numberofwords++;
			i++; 
			// extra i++ makes it skip double spaces, or space/return
		}
		if (content.substring(i,i+1) == "n") {
			numberofwords++;
			i++;
			// extra i++ makes it skip double spaces, or space/return

		}

		i++;
	}

	return numberofwords;
}

// -->
</SCRIPT>

Body Code:

<form>
<textarea  cols=50 rows=5 onChange="this.form.sizebox.value=this.value.length; this.form.sizeboxw.value=words(this.value)"></textarea><br><input name=sizebox value=0 size=4> characters,<input name=sizeboxw value=0 size=4> words
<p><font size=+1>Write smth on the field then click outside of text box to recompute.</font>

Times Viewed: 7

Leave a Reply

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