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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.