Validate form before submit jquery

You can mimic the default jQuery plugin behaviour as follows:

Test if the form is valid; if not prevent the default action from occurring using preventDefault():

$(document)
.on('click', 'form button[type=submit]', function(e) {
    var isValid = $(e.target).parents('form').isValid();
    if(!isValid) {
      e.preventDefault(); //prevent the default action
    }
});

EDIT: this comes in handy if you want to fine-tune your validation test i.e. validating some controls conditionally.

Leave a Comment