Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent form submission on Enter key press

I have a form with two text boxes, one select drop down and one radio button. When the enter key is pressed, I want to call my JavaScript function, but when I press it, the form is submitted.

How do I prevent the form from being submitted when the enter key is pressed?

like image 786
Shyju Avatar asked May 25 '09 03:05

Shyju


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 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.

How Prevent form submit on enter key press JQuery?

getElementById("testForm"); form. addEventListener("submit",function(e){e. preventDefault(); return false;}); This solution will now prevent the user from submit using the enter Key and will not reload the page, or take you to the top of the page, if your form is somewhere below.


2 Answers

if(characterCode == 13) {     return false; // returning false will prevent the event from bubbling up. } else {     return true; } 

Ok, so imagine you have the following textbox in a form:

<input id="scriptBox" type="text" onkeypress="return runScript(event)" /> 

In order to run some "user defined" script from this text box when the enter key is pressed, and not have it submit the form, here is some sample code. Please note that this function doesn't do any error checking and most likely will only work in IE. To do this right you need a more robust solution, but you will get the general idea.

function runScript(e) {     //See notes about 'which' and 'key'     if (e.keyCode == 13) {         var tb = document.getElementById("scriptBox");         eval(tb.value);         return false;     } } 

returning the value of the function will alert the event handler not to bubble the event any further, and will prevent the keypress event from being handled further.

NOTE:

It's been pointed out that keyCode is now deprecated. The next best alternative which has also been deprecated.

Unfortunately the favored standard key, which is widely supported by modern browsers, has some dodgy behavior in IE and Edge. Anything older than IE11 would still need a polyfill.

Furthermore, while the deprecated warning is quite ominous about keyCode and which, removing those would represent a massive breaking change to untold numbers of legacy websites. For that reason, it is unlikely they are going anywhere anytime soon.

like image 92
Josh Avatar answered Sep 16 '22 11:09

Josh


Use both event.which and event.keyCode:

function (event) {     if (event.which == 13 || event.keyCode == 13) {         //code to execute here         return false;     }     return true; }; 
like image 38
Agiagnoc Avatar answered Sep 18 '22 11:09

Agiagnoc