Disable the Enter Key with Javascript

When filling out an online form, by default, if you are on an element of the form and press the Enter key the form will submit. Sometimes you want this disabled. There are many reasons why you would want this disabled, most of the time it’s simply user error in that they think they can press the enter key to make it to the next element rather than the tab key.

There is a simple Javascript workaround to disabling the enter key on your forms so that this doesn’t happen and so that the user must click the submit button with their mouse. See the code below.

<script type=”text/javascript”>
function stopRKey(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 = stopRKey;
</script>