Jump to content

text area word count


poe

Recommended Posts

i have 3 radio buttons, each one will set the max number of words that can be inputted into the "desc" form textarea. i think i am almost there except when the person reaches the limit of words, you are still able to type spaces.

 

<script language="JavaScript">
function cntWd(field,wordcount,balance,maxwd){
var y = field.value;
var max = maxwd.value;
var r = 0;
a = y.replace(/\s/g,' ');
a = a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}

var bal = maxwd.value - wordcount.value;

if(max < r) {
field.value = field.value.substring(0, field.value.length-1);
} else {
wordcount.value = r;
balance.value = maxwd.value - wordcount.value;
}
}
</script>

 

and the form is:

<table border=0>
<tr><td>
<form name="myform">

<input type=radio name=adplan value=20 onClick="setMax(this,document.myform.maxwd)">20 word max<br>
<input type=radio name=adplan value=10 onClick="setMax(this,document.myform.maxwd)">10 word max<br>
<input type=radio name=adplan value=5 onClick="setMax(this,document.myform.maxwd)">5 word max<br>



<textarea rows="6" name="desc" cols="30" onkeyup="cntWd(this,document.myform.wordcount,document.myform.balance,document.myform.maxwd)"></textarea><br>
<input type="text" name="wordcount" value="0" size="5" onkeyup="cntWd(document.myform.desc,this,document.myform.balance,document.myform.maxwd)"> : total words<br>
<input type="text" name="balance" value="0" size="5"> : balance<br>
<input type="text" name="maxwd" size="5"> : Max<br>

</form>
</td></tr>
</table>
</center>

 

Link to comment
https://forums.phpfreaks.com/topic/69399-text-area-word-count/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.