[thelist] Form Submittal on Enter

.jeff jeff at members.evolt.org
Mon Jan 28 14:28:01 CST 2002


josh,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: josh
>
> I have noticed that IE4.0 (and perhaps others) will
> submit a form when the user hits the "Enter" key even
> when there is more than one field in the form.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

and for those users using browsers that submit forms with the "enter" key,
they're probably used to that behavior.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> Do any of you have Javascript solutions to prevent the
> submission of the form unless the Submit button was
> pressed?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

if they're used to the default behavior, why would you want to take that
away?  what if they have javascript disabled?  then what?

suggestion -- forget about the "enter" key entirely.  if you don't want them
to submit the form early then use the onsubmit event handler to validate the
form and halt submission if it's insufficient/invalid.

function validateForm(form)
{
  if(!form.username.value)
  {
    alert('Username is required.');
    form.username.focus();
    return false;
  }
  if(!form.password.value)
  {
    alert('Password is required.');
    form.password.focus();
    return false;
  }
  return true;
}

and called from the onsubmit event handler like this:

<form
 action="login.cfm"
 method="post"
 onsubmit="return validateForm(this)">

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/





More information about the thelist mailing list