Advertisement
ASP_Volume3 Internet/ Browsers/ HTML #53438

Prevent Enter Key from Submitting your Forms

When searching online for "How to prevent a form to be submitted by hitting enter" you'll find many (mostly unsatisfying) creative solutions. I think that my solution will work for most of the cases. Just follow the neccessary steps in the following article. If you like it please vote! ;)

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
<font size=2 family=courier><br><b>Step 1 - Adjusting the form tag</b><br>
The form tag has to contain the 'onsubmit' attribute specified as follows: onsubmit="return (1==2)"<br>
Why not use 'false' instead of '(1==2)'? This is just to avoid troubles with scripting-engines in different languages... sad indeed;)<br><br>
<b>Step 2 - Changing input types</b><br>
The input type for submit buttons on your form has to be changed from 'submit' to 'button'. Besides that you have to give the button the 'onclick' attribute specified as follows: onclick="document.yourformname.submit()"<br>
These two steps can be enough unless you did use the 'value' attribute to send some extra information with the former submit button. Therefore we have step 3.<br><br>
<b>Step 3 - Add hidden input</b><br>
Let's say you named all your submitbuttons 'SubmitAction' and specified a different value for each button. This will not work anymore since we changed to 'button'-buttons instead of 'submit'-buttons. So in this case you should add an extra input tag (type=hidden name=SubmitAction) and add a little something to the 'onclick' tags in step 2. Change it from 'onclick="document.yourformname.submit()"' to 'onclick="document.yourformname.SubmitAction.value='whatever';document.yourformname.submit()"'<br><br>That was all. Good luck to you all and I hope you find this article usefull.</font>
Original Comments (3)
Recovered from Wayback Machine