Forum Moderators: open

Message Too Old, No Replies

JavaScript Word Counter

         

cbeyer

8:59 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Here is the code that I found online to count the words a user has left in a textarea.


// Word Count
var submitcount=0;
function checkSubmit() {
if (submitcount == 0)
{
submitcount++;
document.Surv.submit();
}
}
function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
if (field.value.charAt(x) == " " && field.value.charAt(x-1)!= " ") {wordcounter++} // Counts the spaces while ignoring double spaces, usually one in between each word.
if (wordcounter > 250) {field.value = field.value.substring(0, x);}
else {countfield.value = maxlimit - wordcounter;}
}
}
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
{field.value = field.value.substring(0, maxlimit);}
else
{countfield.value = maxlimit - field.value.length;}
}

Here are the textarea fields...


<textarea name="Q3367" cols="50" rows="4" wrap="hard" onKeyDown="wordCounter(this.form.Q3367,this.form.remLen,250);" onKeyUp="wordCounter(this.form.Q3367,this.form.remLen,250);"></textarea>
<br><div style="margin-left: 270px;">Words remaining: <input type="text" name="remLen" value="250" size="3" readonly></div>

Problem is, it doesn't count a word when a user hits return. How do I also account for the carriage returns as well as the white spaces?

Thanks,
C

Fotiman

5:11 pm on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's one way you could do it. In this example I'm using a RexExp to determine if it's a whitespace character (/\s/ will match any whitespace character including newlines, tabs, etc.).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
// Word Count
function wordCounter(field, countfield, maxlimit) {
var wordcounter = 0;
var wordstarted = false;
var whitespaceReg = /\s/; // Any whitespace character
var i, c;
for (i = 0; i < field.value.length; i++) {
c = field.value.charAt(i);
if (!wordstarted) { // looking for non-whitespace to start the word
if (!c.match(whitespaceReg)) {
wordstarted = true;
wordcounter++;
if (wordcounter > maxlimit) {
// We reached the limit, no more words allowed
// Strip everything from this character on
field.value = field.value.substring(0, i);
wordcounter--;
break;
}
}
else {
continue;
}
}
else { // looking for whitespace to end the word
if (c.match(whitespaceReg)) {
wordstarted = false;
}
else {
continue;
}
}
}
countfield.value = maxlimit - wordcounter;
}
</script>
<form action="">
<textarea name="Q3367" cols="50" rows="4" wrap="hard"
onKeyDown="wordCounter(this,this.form.remLen,3);"
onKeyUp="wordCounter(this,this.form.remLen,3);"></textarea>
<br><div style="margin-left: 270px;">Words remaining:
<input type="text" name="remLen" value="3" size="3" readonly></div>
</form>
</body>
</html>

Note, however, that this method still isn't extremely efficient. With each key struck, it's going to loop through the entire contents of the textarea twice (once on keydown and once on keyup). Crude, but effective.

mehh

8:42 pm on Dec 28, 2007 (gmt 0)

10+ Year Member



This is what I got:
function wordCounter(field, countfield, maxlimit) { 
var words=field.value.split(/[^\w\d-]+/g);
for(var i=0;i<words.length;i++){
if(!words[i]){
words.splice(i,1);
i--;
}
}
countfield.value = maxlimit - words.length;
}
It splits the value of the text feild into an array at any charictor that isn't a word charictor then "cleans" the array of empty values. The end result is a list of all the used words so you can just use the length property.

[edit] Changed the regex to include digits and hyphens