HighDots Forums  

character counter

Javascript JavaScript language (comp.lang.javascript)


Discuss character counter in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
reneecccwest
 
Posts: n/a

Default character counter - 10-07-2003 , 11:26 AM






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

Reply With Quote
  #2  
Old   
Paul Wellner Bou
 
Posts: n/a

Default Re: character counter - 10-07-2003 , 11:56 AM






reneecccwest wrote:

Quote:
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)

Quote:
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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.