Monday, 17 October 2016

Disallow enter key anywhere in form

Disallow enter key anywhere in form

If you don't have a <textarea> in your form, then just add the following to your <form>:
<form ... onkeypress="return event.keyCode != 13;">
 
Or with jQuery:

$(document).on("keypress", "form", function(event) { 
    return event.keyCode != 13;
});
 
 
This will cause that every key press inside the form will be checked on the keyCode. If it is not 13 (the Enter key), then it will return true and anything will go as expected. If it is 13 (the Enter key), then it will return false and anything will stop immediately, so the form won't be submitted.

-banu-

0 comments:

Post a Comment