Doug Cassidy
Doug Cassidy

Reputation: 1907

jQuery.validate() firing on button clicks (I want only on submit)

The end of my form is like:

<input type="submit" name="login" value="Login" class=" btn btn-primary" >    
<a href="register.php" ><button class="btn btn-primary" >Register</button></a>    
<a href="/"><button class="btn btn-primary" >Cancel</button></a>
</form>

then:

jQuery("form#login").validate({
    submitHandler: function(form) {
        $("#loading").modal();
        form.submit();
    }
});

When you click the login button, the form validates, as expected. However, the form is also validating on click of either of the two buttons, in the form. These other two buttons are just links, I need them in the form, so that I can display them all in a row.

Probly, it's to do with validate() firing on :submit, which is any button, in the form. How can I tell it only validate on an actual click of the submit button?

Upvotes: 1

Views: 857

Answers (1)

Leo Farmer
Leo Farmer

Reputation: 7910

You need to give the buttons a type='button'

e.g.

<button class="btn btn-primary" type="button" >Cancel</button>

This is because, if you don't set it, some browsers default it to type='submit'.

Upvotes: 6

Related Questions