views:

134

answers:

5

I have a form where I've specified onSubmit="validate()", but I want to ignore the validation if the submit-button was clicked. Is there a good cross-browser way of detecting if the submit button was clicked and thus ignoring the validation?

+1  A: 

Why don't you use a button instead of a submit, and set it's action on the click of the button? That way you can control if you want to validate, submit, or whatever else you like.

Marcos Placona
buttons don't degrade for non-javascript users.
geocar
make a dynamic button? Replace your submit with it.
drozzy
@geocar do we really need to think that buttons don't work for non-js users when we;re talking about: onSubmit="validate()"?
Marcos Placona
@mplacona input type=button *cannot* submit the form for a non-js user while input type=submit can. non js users simply ignore the onsubmit="" handler anyway. it's up to the questioner if he cares about usability for people with javascript disabled or nonpresent.
geocar
A: 

Did you try this?

<input type="submit" onclick="void(window.validate=function(){return true;})" value="Submit" />
geocar
+1  A: 

you can try to use a <input type="button"... with an onClick that submits the form - a javascript .submit() doesn't fire the onSubmit-function of the form.

oezi
A: 

Just return false, or preventDefault from your submit button handler

Juan Mendes
That will stop the form submitting, not ignore the validation.
David Dorward
oops, that's true, misunderstood the question
Juan Mendes
A: 

The submit event only fires if the form is submitted by the user; not if it is submitted via JS.

Therefore:

<input type="submit" onclick="this.form.submit(); return false;">

If JS is not available, this acts like a normal submit button … and the onsubmit still fails to fire as it also requires JS.

(Attaching events using JS instead of intrinsic event attributes is, as usual, preferred by excluded from this example for the sake of clarity)

David Dorward