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>

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>

can you just do it my way, please, and just tell me what the error is?

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.