Er. alokpandey's Blog

How to disable the Enter key on HTML form

Posted in Uncategorized by Alok Kumar Pandey on September 12, 2010

Normally when you have a form with several text input fields, it is undesirable that the form gets submitted when the user hits ENTER in a field. Some people are pressing the enter key instead of the tab key to get to the next field. They often do that by accident or because they are accustomed to terminate field input that way. If a browser regards hitting ENTER in a text input field as a request to submit the form immediately, there is no sure way to prevent that.

Add the below script to the section of your page. The following code disables the enter key so that visitors of your web page can only use the tab key to get to the next field.

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;

Advertisements
Tagged with: