Philipp Philipp - 1 month ago 6
jQuery Question

use jQuery form validation equalTo rule for dynamically generated forms

I have n forms on one page. Each one has two email address fields (normal field and verification). What I want to do is to set a equalTo rule for the two fields. What I am doing now is this:

$(".contactform").validate({
onsubmit: true,
onkeydown: false,
onkeyup: false,
onfocusin: false,
onfocusout: false,
onchange: false,
onclick: false,
rules: {
email_veri: {
equalTo: ".email"
}
}
});


But this is obviously not working as the id of email and email_veri is different in every form. I read that it is possible to set a validation rule directly on a field but I didn't find out how. I hope someone can help.

Answer Source

This may be useful, from "jQuery Validation with multiple (mostly) identical forms" at http://www.epalla.com/2009/12/jquery-validation-with-multiple-forms/

Their answer is to validate each form separately:

$(".question_form").each(function() {
    $(this).validate(
        rules: {
            email_veri: {
                equalTo: ".email"
        }
    );
});