Daniel Allen Langdon Daniel Allen Langdon - 1 year ago 208
HTML Question

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())

How do I do this?

Answer Source

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 false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download