简体   繁体   中英

jQuery validate input if element hasClass

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!

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM