Press enter instead of click on form

liunx

Guest
On my site, I have a basic password section, though I wish to make it so when you press ENTER it automatically logs on, rather than having to click on the log on button myself. The code is below<br />
<br />
<script language="javascript"><br />
<!--//<br />
function pasuser(form) {<br />
if (form.id.value=="theirusername") { <br />
if (form.pass.value=="theirpassword")<br />
location="http://blahblahblah.com" <br />
else alert("Invalid Password")<br />
}<br />
else alert("Invalid Username") <br />
}<br />
//--><br />
</script><br />
<form name="login"><br />
User: <input name="id" type="text"><br><br />
Password: <input name="pass" type="password"><br><br />
<input type="button" value="Login" onClick="pasuser(this.form)"><br />
</form><!--content-->I could be wrong, but I believe if you have an input type button instead of input type submit, hitting enter will not submit the form. You will need a few modifications to make this work and keep your validation.<br />
<br />
First, in your <form> tag, add target="blah.com". You should also add your method type (post or get), but it's not critical to making this work. Change your button input type to a submit, and add return to your onClick event so it will look like this:<br />
<br />
<input type="submit" value="Login" onClick="return pasuser(this.form)"><br />
<br />
<br />
In your JavaScript code, instead of the location =... line, change it to return true, and after your alert statements, add return false like this:<br />
<br />
<!--//<br />
function pasuser(form) {<br />
if (form.id.value=="theirusername") {<br />
if (form.pass.value=="theirpassword") {<br />
return true;<br />
} else {<br />
alert("Invalid Password");<br />
return false;<br />
}<br />
} else {<br />
alert("Invalid Username") ;<br />
return false;<br />
}<br />
}<br />
//--><!--content-->Thanks a heap :D<!--content-->
 
Back
Top