On submission of 2nd form, first form is being validated - jquery validate

387 views Asked by At

On submission of 2nd form, first form is being validated ,both forms are independent having separate submit buttons and both are validated using jquery validator plugin.

here is my code :

$("#addVehicleForm,#discountInfoForm,#ihqform,#saveReturnForm,#driversForm").validate({
// stuff here

)}

#saveReturnForm,#driversForm these are the id's which i am passing in same validator plugin method.

I am trying to validate by click like this :

$("#my-submit-button-name").click(function(){
$(this).submit();
})

Note : Both form has different submit button

2

There are 2 answers

0
Craig Harshbarger On

I use jQuery Validate on almost every project. I wrote the following statement to run validation on every form. The key is the each() statement. The each() statement allows you to run validate() on each form element separately.

$('form').each(function(){

    $(this).validate({

        // stuff here

    });

});

Or in your case

$('#addVehicleForm, #discountInfoForm, #ihqform, #saveReturnForm, #driversForm').each(function(){

    $(this).validate({

        // stuff here

    });

});

Also, if you have custom buttons to submit the forms you can do this. But you should probably just use <input type='submit' value='Submit' />

$("#my-submit-button-name").on('click', function(){
    $(this).parents('form').submit();
})
3
Nikhil Batra On

They are bound to the same event, so each form will be called. Instead use this:

$("#addVehicleForm,#discountInfoForm,#ihqform,#saveReturnForm,#driversForm").click(function(){
$(this).validate({  //Do your work here )}

)}

});