Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to prevent enter button submitting a form

Tags:

I've seen a lot of Javascript solutions to do this, but I'm sure there must be an easier way.

I have a really simple form - one multiline textbox and a submit button. I want the user to be able to submit "formatted" text (i.e. like an email, with paragraphs, new lines etc)

However, when the user hits Enter to put in a carriage return it submits the form.

I'm there there must be a property or something that controls this, as this must be a common issue. Javascript as a solution seems a bit too complex.

like image 934
Ben Avatar asked Apr 21 '11 09:04

Ben


People also ask

How do I stop form submission on enter key?

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 do you stop re submitting a form after clicking back button?

You can check if the user clicked the back button, disable form if true. Another way is by storing a cookie which you check on page load, if it exists you can disable the form.

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

Set the UseSubmitBehavior property on the submit button to FALSE. (found the solution here)

like image 176
Max Avatar answered Oct 07 '22 08:10

Max


The following code solved my issue:

http://www.itorian.com/2012/07/stop-from-submission-posting-on-enter.html

<script type="text/javascript">     //Stop Form Submission of Enter Key Press     function stopRKey(evt) {         var evt = (evt) ? evt : ((event) ? event : null);         var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);         if ((evt.keyCode == 13) && (node.type == "text")) { return false; }     }     document.onkeypress = stopRKey; </script> 

Put this in the master page or just in the pages that you want.

like image 20
alansiqueira27 Avatar answered Oct 07 '22 10:10

alansiqueira27