reneecccwest wrote:
Hello, can anybody share the code for a word counter for the textarea
user input? what if max characters are 256?
thanks
Assuming a "word" is any sequence of non-whitespace characters surrounded
by whitespace, the following will count the words in a text area:
<form>
<textarea name="myTextare a"></textarea>
<input type="button" name="myButton" value="Count words"
onclick="countW ords(this.form. myTextarea.valu e);">
</form>
<script type="text/javascript">
function countWords(s) {
var words = s.split(/\s+/);
alert(words.len gth);
}
</script>
For preventing entry of more then 256 characters, you can use the
following. Be aware you should provide server-side validation as well,
since the client-side prevention shown below can easily be circumvented:
<textarea name="myTextare a" onkeydown="retu rn maxChars(this,
256);"></textarea>
<script type="text/javascript">
function maxChars(ta, count) {
return (ta.value.lengt h < count);
}
</script>
--
| Grant Wagner <gw*****@agrico reunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
*
http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
*
http://www.mozilla.org/docs/web-deve...upgrade_2.html