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 clicking the submit button. Is there a way to prevent this?
I'm using HTML, PHP 5.2.9, and jQuery on the survey.
Something I have not seen answered here: when you tab through the elements on the page, pressing Enter when you get to the submit button will trigger the onsubmit handler on the form, but it will record the event as a MouseEvent. Here is my short solution to cover most bases:
This is not a jQuery-related answer
HTML
JavaScript
To find a working example: https://jsfiddle.net/nemesarial/6rhogva2/
Use:
This solution works on all forms on a website (also on forms inserted with Ajax), preventing only Enters in input texts. Place it in a document ready function, and forget this problem for a life.
Not putting a submit button could do. Just put a script to the input (type=button) or add eventListener if you want it to submit the data in the form.
Rather use this
than using this
If you use a script to do the actual submit, then you can add "return false" line to the onsubmit handler like this:
Calling submit() on the form from JavaScript will not trigger the event.
You can use a method such as
In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:
Giving the form an action of 'javascript:void(0);' seems to do the trick