1

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!

1 Answer 1

1

"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

Sign up to request clarification or add additional context in comments.

2 Comments

Ah excellent! Thank you! I wasn't aware there was an ignore option. That will work perfectly. I will accept the answer as soon as I am able
@TyBailey, FWIW, every available option is outlined on that page I linked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.