<form ... onkeypress="returnonkeydown="return event.keyCodekey != 13;">'Enter';"> $(document).on("keypress""keydown", "form", function(event) { return event.keyCodekey != 13;"Enter"; }); This will cause that every key press inside the form will be checked on the keyCodekey. If it is not 13 (the Enter key)Enter, then it will return truetrue and anything will gocontinue as expectedusual. If it is 13 (the Enter key)Enter, then it will return falsefalse and anything will stop immediately, so the form won't be submitted.
The keypresskeydown event is preferred over keydownkeyup as this is only fired when the character is actually being inserted. The keydown (and keyup) are fired when any key is pressed, including control keystoo late to block form submit. AndHistorically there was also the keypress, but this is deprecated, as is the keyCode ofKeyboardEvent.keyCode. You should use keypress representsKeyboardEvent.key instead which returns the actual character being inserted, notname of the physical key used. This way you don't need to explicitly check if Numpad Enter key (108) isbeing pressed too. TheWhen keyupEnter is too late to block form submitchecked, then this would check 13 (normal enter) as well as 108 (numpad enter).
Note that $(window) as suggested in some other answers instead of $(document) doesn't work for keydown/keypress/keyup in IE<=8, so that's not a good choice if you're like to cover those poor users as well.
If you have a <textarea> in your form (which of course should accept the Enter key), then add the keypresskeydown handler to every individual input element which isn't a <textarea>.
<input ... onkeypress="returnonkeydown="return event.keyCodekey != 13;">'Enter';"> <select ... onkeypress="returnonkeydown="return event.keyCodekey != 13;">'Enter';"> ... $(document).on("keypress""keydown", ":input:not(textarea)", function(event) { return event.keyCodekey != 13;"Enter"; }); $(document).on("keypress""keydown", ":input:not(textarea)", function(event) { if (event.keyCodekey == 13"Enter") { event.preventDefault(); } }); $(document).on("keypress""keydown", ":input:not(textarea):not(:submit)", function(event) { // ... });