monkeyUser monkeyUser - 5 months ago 14
jQuery Question

Validate form after Click with jquery validate

I have 3 forms in the same page and I want to validate when I click the button, but I have to click twice.

$(document).ready(function () {

$(".form_foo").submit(function (e) {
salvaformRisposta($(this).attr('id'));
e.preventDefault(e);
});
});

function validateForm(form_id) {
$("#"+form_id).validate({
rules: {
},
submitHandler: function (form) {
salvaformRisposta(form);
}
});
}


UPDATE:
I resolved with

$(".form_foo").each(function(key, form) {
validateForm(form.id);
});
//
$(".form_foo").submit(function (e) {
console.dir($(this).attr('id'));
validateForm($(this).attr('id'));
e.preventDefault(e);
});

Answer

You can do something like that

$(document).ready(function(){
    $('#your_form_id').validate({
        rules: {
            ....
        }, messages: {
             ...
        }, submitHandler: function (form) {
         //insert here your code if validation is ok
       }
    }
});

insert this snippet in a forEach