SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Textarea Input Length Limiter - Question

    I've created a simple mootools script that limits a textarea text size to a certain number. The way that it works is that everytime a "keypress" or "keydown" event is called, the event and the "newly entered character" are caught (with mootools' helpful event object).

    So when the character is fetched, it is compared to see whether it is a left, right, up, down, backspace, home, end, or delete operation. CTRL and ALT operations are also looked after;
    Everthing else either is a character or some other command that doesn't add a character. Also when there is a selection then that is also examined, because the selection simply gets replaced by a character.

    If the total amount of characters exceeds the limit then the event is simply stopped (event.preventDefault, event.stopPropragation).

    This all works, but, I'm just wondering, is this consistent across operating systems? Browsers? Keyboards?
    I can't believe I ate the whole thing

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just look at the length of the texarea value on keyup and onchange, and truncate anything over the limit with substring.

    A benefit of this, besides simplicity, is that it will catch user input from a paste operation to the textarea.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    It is difficult to get a consistant cross-browser solution.

    Here's one that I did recently to validate if the entered character was a valid character.
    http://www.sitepoint.com/forums/show...7&postcount=24
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •