469,607 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

character counter

Please discard the previous message. It should be characters instead of words.
can anybody share the code for a character counter for the textarea
user input? what if max characters are 256?

thanks
Jul 20 '05 #1
1 4185
reneecccwest wrote:
Please discard the previous message. It should be characters instead of words.
can anybody share the code for a character counter for the textarea
user input?
Just use the length property of strings.

document.forms['formname_or_index'].textareaname.value.length

(Tested in IE6, Opera7, Mozilla Firebird, Netscape4.7)
what if max characters are 256?


What do you mean?
You want that the user is not able to type more
characters than 256 into the textarea?

<textarea name="textareaname"
onkeydown="check_chars(256);"></textarea>

And The Function:

function check_chars(iMax)
{
textarea = document.forms['formname_or_index'].textareaname;
if(textarea.value.length >= iMax)
{
// In some Browsers this would work fine:
// return false;
// As it don't work in all, take this:
textarea.value = textarea.value.substr(0, iMax-1);
}
}

This should work fine.

Saludo
Paul.
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by chandy | last post: by
5 posts views Thread by Magix | last post: by
16 posts views Thread by thenightfly | last post: by
2 posts views Thread by Thejan Mendis | last post: by
7 posts views Thread by libsfan01 | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.