Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cancel form submission in submit button onclick event?

I'm working on an ASP.net web application.

I have a form with a submit button. The code for the submit button looks like <input type='submit' value='submit request' onclick='btnClick();'>.

I want to write something like the following:

function btnClick() {     if (!validData())         cancelFormSubmission(); } 

How do I do this?

like image 852
Vivian River Avatar asked Nov 19 '10 16:11

Vivian River


People also ask

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 do I stop form submit when onclick event return false?

Use the return value of the function to stop the execution of a form in JavaScript. False would return if the form fails to submit.

How do I stop a form submission?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting. Example: HTML.


1 Answers

You are better off doing...

<form onsubmit="return isValidForm()" /> 

If isValidForm() returns false, then your form doesn't submit.

You should also probably move your event handler from inline.

document.getElementById('my-form').onsubmit = function() {     return isValidForm(); }; 
like image 101
alex Avatar answered Sep 20 '22 22:09

alex