jQuery Validator Validate by class name

Here I have found two solutions to achieve this, and I share it with you all expecting it will help to save someone's time :)
Tip: Opt out your common rules and enter the other rules inside the $().validate() as usual. You can enter common rules inside below methods after this.
Method 01: Using jQuery Validator’s .rules() method.
Ex : I want to validate all the price fields (having class “.price”).$('.price').each(function() {
$(this).rules("add", {
required: true,
min: 0
});
});
Note: You can use this only after the $().validate() method is called. Otherwise it will not work.
Method 02: Using the addClassRules() method.
This method is developed mainly for this type of validations. It does the iteration through all the elements by itself, which we did manually in method 01.$.validator.addClassRules("price", {
required: true,
minlength: 2
});
Note: Unlike in method 01, you don't need this method to be after the $().validate() always. You can even call this method before the $().validate().
Please leave a comment and let me know if this was helpful to you.
0 comments :
Post a Comment