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?
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.
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.
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.
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(); };
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