user1934428
user1934428

Reputation: 22291

My Onclick handler returns false, but my form is still submitted

I have a form with a submit button, and a validator attached to it via onclick=.... When I click on the submit button, the validator is executed and returns false. Still, the form is submitted. How can this be?

To narrow the problem further, I even changed the code temporarily to:

<form id="contactform" method="post" action="cgi-bin/process_contact_form.rb">
    ....
    <input type="submit" value="Abschicken" onclick="false && k_validate('de')" />
</form>

This would just return false and bypass my validator. Indeed, even in this version, the form gets submitted.

Note that I am aware that there are means to get a form submitted with bypassing the validator. I am here only interested in the "normal" case, i.e. when a user explicitly clicks the button.

I have tested this on Mac OSX 10.6.8, using the following browsers:

SeaMonkey 2.0.14 Safari 5.1.10 Chrome 36.0

JavaScript is, of course, enabled.

The test page with the aforementioned "false" shortcut can be seen here:

http://www.tsukaeru-doitsugo.de/testing.html

It has two buttons. The first button (labelled "Abschicken") is the one within the form. Its onclick handler returns false, but the form is still submitted.

Upvotes: 3

Views: 2814

Answers (1)

Esko
Esko

Reputation: 4207

You are missing the return-statement:

<input type="submit" value="Abschicken" onclick="return k_validate('de')" />

Upvotes: 7

Related Questions