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 need to change
to
and
to
That said, I'd try to avoid the intrinsic event attributes in favour of unobtrusive JS with a library (such as YUI or jQuery) that has a good event handling API and tie into the event that really matters (i.e. the form's submit event instead of the button's click event).
It's simple, just return false;
The below code goes within the onclick of the submit button using jquery..
Why not change the submit button to a regular button, and on the click event, submit your form if it passes your validation tests?
e.g
Change your input to this:
And return false in your function
Sometimes
onsubmit
wouldn't work with asp.net.I solved it with very easy way.
if we have such a form
You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.