Question

I am using the jQuery validation plugin to validate a checkout form on an ecommerce site. The validation works great, however I only need to validate inputs that don't have the class no-validate

Can I use the depends method to do this check? For example, would something like this work:

checkoutForm.validate({
    rules: {
        firstname: {
            required: {
                depends: function(element) {
                    return element.not('.no-validate');
                }
            }
        }
    }
});

Or do I have to do something different to do this check? I thought about wrapping the entire rules array in a conditional like:

if(!$(checkoutForm + ' input').hasClass('no-validate') { rules { //rules here } }

but I would rather use the depends method if possible.

Any help/tips would be greatly appreciated!

Was it helpful?

Solution

"The validation works great, however I only need to validate inputs that don't have the class no-validate"

Use the ignore option to ignore those…

checkoutForm.validate({
    ignore:  '.no-validate',
    rules: {
        firstname: {
            required: true
        }
    }
});

See: http://jqueryvalidation.org/validate/#ignore

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top