JAVASCRIPT » Javascript DHTML

  Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

How to disable the Enter key on HTML form


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 <head> 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.

  <script type="text/javascript">

function stopEnterKey(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 = stopEnterKey;

 

</script>


Free   Version: n/a   Platform(s): All   Updated:  April 10, 2008

Developer:Navioo.com Demo Download  
Rated by: 1 user(s)  
Follow Navioo On Twitter

Submit a resource