I have a survey on a website, and there seems to be some issues with the users hitting enter (I don\'t know why) and accidentally submitting the survey (form) without clicki
I'd like to add a little CoffeeScript code (not field tested):
$ ->
$(window).bind 'keypress', (event) ->
if event.keyCode == 13
unless {'TEXTAREA', 'SELECT'}[event.originalEvent.srcElement.tagName]
event.preventDefault()
(I hope you like the nice trick in the unless clause.)
If you don't have a <textarea>
in your form, then just add the following to your <form>
:
<form ... onkeydown="return event.key != 'Enter';">
Or with jQuery:
$(document).on("keydown", "form", function(event) {
return event.key != "Enter";
});
This will cause that every key press inside the form will be checked on the key
. If it is not Enter
, then it will return true
and anything continue as usual. If it is Enter
, then it will return false
and anything will stop immediately, so the form won't be submitted.
The keydown event is preferred over keyup as the keyup
is too late to block form submit. Historically there was also the keypress, but this is deprecated, as is the KeyboardEvent.keyCode. You should use KeyboardEvent.key instead which returns the name of the key being pressed. When Enter
is checked, 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
/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 keydown handler to every individual input element which isn't a <textarea>
.
<input ... onkeydown="return event.key != 'Enter';">
<select ... onkeydown="return event.key != 'Enter';">
...
To reduce boilerplate, this is better to be done with jQuery:
$(document).on("keydown", ":input:not(textarea)", function(event) {
return event.key != "Enter";
});
If you have other event handler functions attached on those input elements, which you'd also like to invoke on enter key for some reason, then only prevent event's default behavior instead of returning false, so it can properly propagate to other handlers.
$(document).on("keydown", ":input:not(textarea)", function(event) {
if (event.key == "Enter") {
event.preventDefault();
}
});
If you'd like to allow enter key on submit buttons <input|button type="submit">
too, then you can always refine the selector as below.
$(document).on("keydown", ":input:not(textarea):not(:submit)", function(event) {
// ...
});
Note that input[type=text]
as suggested in some other answers doesn't cover those HTML5 non-text inputs, so that's not a good selector.
A completely different approach:
<button type="submit">
in the form will be activated on pressing Enter.style="display:none;
false
, which aborts the submission process.<button type=submit>
to submit the form. Just return true
to cascade the submission.<textarea>
or other form controls will behave as normal.<input>
form controls will trigger the first <button type=submit>
, which returns false
, and thus nothing happens.Thus:
<form action="...">
<!-- insert this next line immediately after the <form> opening tag -->
<button type=submit onclick="return false;" style="display:none;"></button>
<!-- everything else follows as normal -->
<!-- ... -->
<button type=submit>Submit</button>
</form>
It is my solution to reach the goal, it is clean and effective.
$('form').submit(function () {
if ($(document.activeElement).attr('type') == 'submit')
return true;
else return false;
});
I had a similiar problem, where I had a grid with "ajax textfields" (Yii CGridView) and just one submit button. Everytime I did a search on a textfield and hit enter the form submitted. I had to do something with the button because it was the only common button between the views (MVC pattern). All I had to do was remove type="submit"
and put onclick="document.forms[0].submit()