Maverick Maverick - 1 year ago 129
jQuery Question

Validate form before submit jquery

I have a html form. i am validating my form using jquery.validate.js and it works on submit event. I want to validate this form before submit get fired.

As per my R&D i have tried :

alert('Before submit performed');

I want to validate form Just before firing submit event.
Can any one tell me how to do this?

Many Thanks,


Answer Source

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

.on('click', 'form button[type=submit]', function(e) {
    var isValid = $('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.