By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,586 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

character counter

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.