How do I prevent the form from being sent by pressing the ENTER button?

In the form of a button with an on-click. You need to prevent the form from being sent by pressing the ENTER button. Is there any way to do this?

Or to make Enter work the same way as pressing a button.

Author: Frontender, 2013-11-23

3 answers

Well, for example, so:

$(document).ready(function() {
      $(form).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
      }
   });
});
 7
Author: Макс Жуков, 2013-11-23 12:47:11

Without using additional libraries such as jQuery, etc., the prohibition of sending a form by pressing the Enter key can be implemented as follows:

<form name="test" action="/">
    <input type="text" name="text1" value="Некоторый текст 1" />
    <br />
    <input type="text" name="text2" value="Некоторый текст 2" />
    <br />
    <input type="button" value="Отправить" onclick="this.parentNode.submit();"> 
</form>

That is, we simply remove input from type="submit", adding input with type="button" and the JS click handler instead. When you press Enter in any field of the form, it will not be sent, because the browser will not be able to detect the send button.

 6
Author: , 2015-12-25 06:42:31
form.addEventListener('keydown', function(event) {
    if(event.keyCode == 13) {
       event.preventDefault();
    }
 });
 1
Author: Иван Образцов, 2016-01-29 22:41:28