How to disabled the enter key in javascript in a web form

Posted on Monday May 09, 2011 / by Eric Potvin

For those who likes to disable the "enter" key when submitting a form, well ... this script is not for you. But for all others who wants to know, simply add the following code to your page.

function disableEnterKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}
document.onkeypress = disableEnterKey;