PDA

View Full Version : Enter on keyboard


simat
11-21-2005, 06:30 AM
I'm, using a form form a password with a button to submit it.
The problem is that i want a user be able to continue by clicking the Enter button on his keyboard, and my code is not doing it.
Here's the code:
<form name="form1">"enter password:"
<input type="password" name="pass">
<input type="button" value="continue" onclick="checkpassword()">
</form>

Any suggestions?
Thanks.

_Aerospace_Eng_
11-21-2005, 06:52 AM
<form name="form1" onsubmit="checkpassword()">"enter password:"
<input type="password" name="pass">
<input type="submit" value="continue">
</form>
I wouldn't really rely on javascript to check a password since it really isn't secure. I recommend a server side language like php or asp.

simat
11-21-2005, 07:09 AM
I made the changes you suggested, but its not working.
I need to be able continue with a click on the button and with an Enter key on keyboard.
Your code not doing either.
Simat

_Aerospace_Eng_
11-21-2005, 07:12 AM
Try onsubmit="return checkpassword()"
Of course I don't know what checkpassword is doing so it may be your function that isn't working.
function checkpassword(){
var password="password";
if(document.forms['form1'].pass.value==password){
alert('Password is valid');
}
else {
alert('Password is invalid');
return false;
}
}

simat
11-21-2005, 07:40 AM
by the way, my function is exactly as you wrote.

_Aerospace_Eng_
11-21-2005, 07:51 AM
This works fine for me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function checkpassword(){
var password="password";
if(document.forms['form1'].pass.value==password){
alert('Password is valid');
window.location='http://www.codingforums.com';
return false;
}
else {
alert('Password is invalid');
return false;
}
}
</script>
</head>

<body>
<form action="#" name="form1" onsubmit="return checkpassword()" method="post">"enter password:"
<input type="password" name="pass">
<input type="submit" value="continue">
</form>
</body>
</html>

simat
11-21-2005, 09:42 AM
:)
I missed the "action="#"....