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
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 .
In a simplest way: $("#myinput"). keydown(function (e) { if(e. which == 13) e. preventDefault(); }); The key is to use "keydown" and event.
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.
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!
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With