3
votes

Unable to validate two hidden fields (Say select an offer and Select any trip) with jQuery Validate plugin. I have two hidden fields in my form. I am using ignore option to validate those two fields. Among the two fields only the second field is validated which is later. How to validate both the fields in a single submit?

 $('.form').validate({
    ignore: ':hidden:not("#off_val")', //ignore for first field
    ignore: ':hidden:not("#trip_ty")',////ignore for second field
    rules: form_rules,
    messages: form_messages,
    errorClass: 'validate',
    errorPlacement: function (err, element) {
        err.insertBefore(element);
    },
    submitHandler: function (form) {
        form.submit();
    }
});
3
Like for any jQuery plugin, you can't declare the same option more than once. Okay to use multiple selectors within the same ignore option.Sparky

3 Answers

10
votes

ignore is a single property which takes only one selector. Merge your not selectors together:

$('.form').validate({
    ignore: ':hidden:not("#off_val, #trip_ty")', //ignore fields
    rules: form_rules,
    messages: form_messages,
    errorClass: 'validate',
    errorPlacement: function (err, element) {
        err.insertBefore(element);
    },
    submitHandler: function (form) {
        form.submit();
    }
});
3
votes

Try this:

$('.form').validate({
    ignore: ':hidden:not("#off_val"),:hidden:not("#trip_ty")',
    rules: form_rules,
    messages: form_messages,
    errorClass: 'validate',
    errorPlacement: function (err, element) {
        err.insertBefore(element);
    },
    submitHandler: function (form) {
        form.submit();
    }
});
3
votes

Try this :

ignore: ':hidden:not("#off_val"),:hidden:not("#trip_ty")',

Make your code like this:

  $('.form').validate({
        ignore: ':hidden:not("#off_val"),:hidden:not("#trip_ty")', //ignore fields
        rules: form_rules,
        messages: form_messages,
        errorClass: 'validate',
        errorPlacement: function (err, element) {
            err.insertBefore(element);
        },
        submitHandler: function (form) {
            form.submit();
        }
    });