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.
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.
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.
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.
Set the UseSubmitBehavior property on the submit button to FALSE. (found the solution here)
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.
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