character counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reneecccwest

    #1

    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
  • Paul Wellner Bou

    #2
    Re: character counter

    reneecccwest wrote:
    [color=blue]
    > 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?[/color]

    Just use the length property of strings.

    document.forms['formname_or_in dex'].textareaname.v alue.length

    (Tested in IE6, Opera7, Mozilla Firebird, Netscape4.7)
    [color=blue]
    > what if max characters are 256?[/color]

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

    <textarea name="textarean ame"
    onkeydown="chec k_chars(256);"> </textarea>

    And The Function:

    function check_chars(iMa x)
    {
    textarea = document.forms['formname_or_in dex'].textareaname;
    if(textarea.val ue.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.

    Comment

    Working...