...

View Full Version : password field limitation



ShMiL
04-14-2005, 10:52 AM
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?

Kor
04-14-2005, 11:16 AM
try using onkeyup

jbot
04-14-2005, 11:19 AM
or onkeypress

ShMiL
04-14-2005, 11:23 AM
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?

glenngv
04-14-2005, 11:29 AM
Delete and backspace keys are not detected on onkeypress.


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

Kor
04-14-2005, 11:33 AM
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)">

ShMiL
04-14-2005, 11:42 AM
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.

glenngv
04-14-2005, 11:49 AM
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?

Kor
04-14-2005, 11:57 AM
Probably he means the alert is shown when using left/right arrows...

ShMiL
04-14-2005, 11:58 AM
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!

glenngv
04-14-2005, 12:17 PM
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.

ShMiL
04-14-2005, 12:36 PM
thanks! :thumbsup: :thumbsup: :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum