Prevent Enter Keypress on HTML Input

Source: https://stackoverflow.com/a/51507806/520848

Section 4.10.22.2 Implicit submission of the W3C HTML5 spec says:

A form element’s default button is the first submit button in tree order whose form owner is that form element.

If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the “enter” key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behavior must cause the user agent to run synthetic click activation steps on that default button.

Note: Consequently, if the default button is disabled, the form is not submitted when such an implicit submission mechanism is used. (A button has no activation behavior when disabled.)

Therefore, a standards-compliant way to disable any implicit submission of the form is to place a disabled submit button as the first submit button in the form:

<form action="...">
  <!-- Prevent implicit submission of the form -->
  <button type="submit" disabled style="display: none" aria-hidden="true"></button>

  <!-- ... -->

  <button type="submit">Submit</button>
</form>

One nice feature of this approach is that it works without JavaScript; whether or not JavaScript is enabled, a standards-conforming web browser is required to prevent implicit form submission.

Alternative 1:

<input type='text' onkeydown='return event.keyCode != 13;' />

Alternative 2:

Source: https://stackoverflow.com/a/1977126/520848

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


<form ... onkeydown="return event.keyCode != 13;">

More solutions: https://stackoverflow.com/questions/895171/prevent-users-from-submitting-a-form-by-hitting-enter