Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    password field limitation

    I use maxlength to limit a password length on an input field.
    The problem is: When a user reaches this limit, he keeps on writing without knowing that his keystrokes are not added to the input.

    I want to alert him when he reaches the limit.
    I tried onKeyDown but had problems with it...

    any idea anyone?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    try using onkeyup
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or onkeypress
    *keep it simple (TM)

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    tried all that
    the problem is that when I reach the limit, I alert the user and then return false for every keypress.
    thus, when a user press backspace or delete, he gets the alert and can't change his password....

    can I detect delete or backspace?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Delete and backspace keys are not detected on onkeypress.

    Code:
    <input type="password" maxlength="8" size="5" onkeypress="if (this.value.length==this.maxLength) alert('You have reached the limit of '+this.maxLength+' characters.')" />
    But why do you set limit to password field? You just give potential hackers a clue on how long the password is, making it easier for them to auto-generate passwords.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    what about:

    <script type="text/JavaScript">
    function limit(v){
    if(v.length>=5){
    alert('You reached the max length')
    }
    }
    </script>

    ...

    <input name="" type="text" maxlength="5" onkeyup="limit(this.value)">
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    glenn and kor
    I've already done this...
    I want to alert only if the user passes the limit, not if he reaches it.
    and if he passed it - i need to alert and not to accept the last keystroke, which means not accepting 'delete' and 'backspace' either.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Have you tested my code? In my code, the maxlength is 8. When the user types 8 characters, the alert is not displayed, on the 9th keystroke, the alert goes up. But if the keystroke is backspace or delete, the alert is not shown. Isn't that what you need?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Probably he means the alert is shown when using left/right arrows...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    Have you tested my code? In my code, the maxlength is 8. When the user types 8 characters, the alert is not displayed, on the 9th keystroke, the alert goes up. But if the keystroke is backspace or delete, the alert is not shown. Isn't that what you need?
    Oh
    I'm very sorry - this is exactly what I need.

    Can anyone refer me to the differences between onkeypress and onkeyup/down?

    Thanks alot glenn!

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Play with this to see the difference of those events.

    <input onkeyup="window.status+='u '" onkeydown="window.status+='d'" onkeypress="window.status+='p'" />

    Try pressing backspace/delete/arrow keys and normal keys and look at the status bar to see which events are triggered and which events are triggered first. Try also pressing a key and delaying the release.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks!


  •  

    Posting Permissions

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