Posts Tagged ‘html form’

How to disable form submission when someone hits the enter key

Saturday, February 28th, 2009

Some times you want to disable the form submission when someone hits the enter key.
Here is how to do it.

// Ignores the enter key to prevent accidental form submission.
// Usage: <input class="" type="text" id="filter" name="filter" value="" onKeyPress="return my_ignore_enter(event);" />
// src: http://jennifermadden.com/javascript/stringEnterKeyDetector.html
// src: http://www.w3schools.com/jsref/jsref_onkeypress.asp
function my_ignore_enter(e) {
var characterCode;

if (window.event) { // IE
characterCode = e.keyCode;
} else if(e.which) { // Netscape/Firefox/Opera
characterCode = e.which;
}

if (characterCode == 13) {
return false;
}

return true;
}

Related