Here is the code that I found online to count the words a user has left in a textarea.
Here are the textarea fields...
Again, this works almost perfectly. When a user is typing and hits the return or enter key (without inserting a space), the word is not counted. I need the javascript to, I guess, check for carriage returns along with white spaces?
Thanks in advance,
C
Code:
// 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...
Code:
<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>
Again, this works almost perfectly. When a user is typing and hits the return or enter key (without inserting a space), the word is not counted. I need the javascript to, I guess, check for carriage returns along with white spaces?
Thanks in advance,
C