reneecccwest wrote:
[color=blue]
> Hello, can anybody share the code for a word counter for the textarea
> user input? what if max characters are 256?
>
> thanks[/color]
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 <gwagner@agrico reunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
Comment