Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 25

Thread: [DHTML] Form Field Limiter

  1. #11
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    One thing annoys me though: the onkeyup event is used, so when the maximum length gets exceeded, the last (disallowed) character still shows up for a moment.
    And when I keep the key pressed, the character keeps being added to the field until the key is released, then the field gets trimed.

  2. #12
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    That's why, in my example I used onkeydown().
    - Mike

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's why, in my example I used onkeydown().
    But then if you hold the key down, it never gets trimmed. The only way to make sure it's always updated is to use setInterval() to check regularly, but this is terribly inefficient.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey View Post
    But then if you hold the key down, it never gets trimmed.
    Why so? The keydown/keypress events keep firing continuously while the key is pressed.

    Edit:
    A simplest example that works:
    HTML Code:
    <input type="text" onkeypress="return this.value.length < 5" />
    Edit:
    Whoops, okay, this example isn't perfect (you can't delete text when field is full), but the onkeypress event still works as expected.
    Last edited by DimX; 04-07-2007 at 08:55 PM.

  5. #15
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Just call the function initially using window.onload or equivalent. Then it will have the desired effect.
    - Mike

  6. #16
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Why so? The keydown/keypress events keep firing continuously while the key is pressed.
    keypress does, keydown doesn't. Also, keypress still suffers from the validation problem: it's only called after the new data is entered. The inability to delete or navigate through text when the field is full is another problem with returning false. If we combine keypress and keyup, we can lessen the appearance of the extra characters (sample updated), but one still appears due to keypress validating before the effect.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #17
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey View Post
    keypress does, keydown doesn't.
    Hmm, in IE and Firefox keydown also does, in Opera not.
    Quote Originally Posted by Twey View Post
    Also, keypress still suffers from the validation problem: it's only called after the new data is entered.
    It's not, it's called before the new data is inserted.

  8. #18
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Er, yes, sorry. Thought one thing and typed another.
    Hmm, in IE and Firefox keydown also does, in Opera not.
    In Konqueror it doesn't either.
    Last edited by Twey; 04-07-2007 at 10:22 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #19
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Works great, but pasting (with CTRL+V and similar methods, probably) gets around it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #20
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Not ctrl-V, but right-clicking and clicking "paste" would. That's not really an issue though, it's not meant to be a secure solution (we'd be in some trouble if it were The only thing it needs to do is provide some indication to the user when they've gone over the limit and it's pointless to type any more.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •