Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent form submission on enter key

Tags:

angularjs

How can I prevent the enter key from submitting the form in angular?

Is there a way to catch the 13 key and disable it or set the form as invalid unless submitting from a button with ID of x?

Thanks

like image 617
Antonio Max Avatar asked Oct 23 '13 18:10

Antonio Max


People also ask

How do I stop form submit on enter key?

Disallow enter key anywhereon("keydown", "form", function(event) { return event. key != "Enter"; }); This will cause that every key press inside the form will be checked on the key .

How do I stop enter key press to submit a web form?

In a simplest way: $("#myinput"). keydown(function (e) { if(e. which == 13) e. preventDefault(); }); The key is to use "keydown" and event.

How do you prevent form submit on enter key press React?

To prevent form submission when the Enter key is pressed in React, use the preventDefault() method on the event object, e.g. event. preventDefault() . The preventDefault method prevents the browser from refreshing the page when the form is submitted.


2 Answers

Since you have ng-click anyways, you could also use <button type="button">, even inside the form tag. The default behaviour of the button element is type="submit", which is what you want to prevent. So, no javascript needed at all!

like image 144
Michael Klöpzig Avatar answered Sep 19 '22 09:09

Michael Klöpzig


Other users have already written that [button type="submit"] will cause this trouble. PLEASE NOTE: buttons WITHOUT any type="..." declaration are "submit" by default! So make sure you always use type="button".

like image 33
Gerfried Avatar answered Sep 19 '22 09:09

Gerfried