0

i am submitting a form in javascript and i want it to submit on key press submit, but it wont work. here is my code:

<script type='text/javascript'>
function login(evt){
    if (evt){var keyCode = evt.which ? evt.which : evt.keyCode;}else{keyCode='';}
    if ((evt == '')||(keyCode == 13)){
	if (document.forms["login"].email.value=='')
	{
		alert('You must enter an email to login!');
	}else if (document.forms["login"].password.value=='')
	{
		alert('You must enter a password to login!');
	}else{
        document.forms["login"].submit();
	}return false;}}
</script>
<form name='login' method='post'>
<tr><td class=status>User: <input type='text' name='email' class='login' id='email' OnKeyDown="return login(event);" style='margin-bottom: 2px;margin-top: 0px; '></td></tr>
<tr><td class=status style='padding-right: 3px;'>Pass: <input type='password' name='password' class='login' OnKeyDown="return login(event);" id='password' style='margin-bottom: 1px; margin-top: 0;'></td></tr>
<tr><td style='padding-right: 3px;'><a href="javascript: login(''); return false" class='loginbutton'>Login</a></td></tr>
</form>
2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Taywin
0

Why are you using 'onkeypress' while the form itself have 'onsubmit' event for you? Also, your HTML code is in malform... A form tag cannot be inside a Table tag that way...

<script type='text/javascript'>
function login(){
  if (document.forms["login"].email.value=='') {
    alert('You must enter an email to login!');
    return false;
  }
  else if (document.forms["login"].password.value=='') {
    alert('You must enter a password to login!');
    return false;
  }

  return true;
}
</script>
<form name='login' method='post' onsubmit="return login()">
<table>  <!-- this must be inside the form tag if you are using it this way -->
<tr><td class=status>
  User: <input type='text' name='email' class='login' id='email' style='margin-bottom: 2px;margin-top: 0px; '>
</td></tr>
<tr><td class=status style='padding-right: 3px;'>
  Pass: <input type='password' name='password' class='login' id='password' style='margin-bottom: 1px; margin-top: 0;'></td></tr>
<tr><td style='padding-right: 3px;'>
  <input type="submit" value="Login"></td></tr>
</table> <!-- close it -->
</form>
0

Hmm...

if (evt){var keyCode = evt.which ? evt.which : evt.keyCode;}else{keyCode='';}
if ((evt == '')||(keyCode == 13)) {
  if (document.forms["login"].email.value=='') {
    alert('You must enter an email to login!');
  }
  else if (document.forms["login"].password.value=='') {
    alert('You must enter a password to login!');
  }
  else {
    document.forms["login"].submit();
  }
  return false;
}

How about your 'keycode' variable is not properly declare? You are declaring the 'keycode' INSIDE a scope of the first if-statement. As a result, outside of the scope, the 'keycode' no longer exists... The script then thinks that 'keycode' is undefined.

Also, changing the keycode value type between integer & string will cause another error in your second check in if-statement. You cannot compare '' with 13. It is inconsistent.

Edited by Taywin: n/a

0

so i should use the code you just gave me? (i am not very good with js)

0

What you might do (if you want to keep your own style) and try it is to take the 'var keycode' outside and above the if(evt). Also, you should do else {keycode=0} instead of keycode==''.

By the way you should not do "evt==''" but rather do only if (evt && ...) something like that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.