Code – Disabling the Enter Key on Web-Forms
Sunday, February 3, 2008 at 5:36AM
Kevin in Code, Code, Forms, JavaScript, JavaScript

An old entry from a few years back, it had a number of hits so it may be useful, qualified for migration...

I got burned a while back by not having this in place when the submit button in a web-app only appeared under certain circumstances. Place this in the top of a web page to squelch the ENTER key in an INPUT=TEXT in your form. Then they gotta use the button when you say that the time is right =)

<script type="text/javascript">

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

document.onkeypress = stopRKey;

</script>
Article originally appeared on KevinGuyer.com - Stranded on Midgard (http://kevinguyer.squarespace.com/).
See website for complete article licensing information.