Monday, November 21

Disable form submission via Enter key

Here are 3 ways to disable form submission by hitting the ENTER key.

1. Submit the form using the onClick event handler instead of onSubmit event handler. It is necessary to return false from the onSubmit event handler to prevent submit on hitting the ENTER key.

2. Create a function that disables the Enter key, and apply it to each input textfield, ie, onkeypress="return noenter()"
Sample javascript code below:

function noenter() {
  return !(window.event && window.event.keyCode == 13);
}


Or

function disableEnterKey(e) {
  var key;
  if(window.event) key = window.event.keyCode; //IE
  else key = e.which; //firefox
  if (key == 13) return false;
  else return true;
}

3. Create a dummy form with a single hidden field.

No comments: