Is it possible to use data-attributes to with the JQuery Validate plugin. I currently use the class name e.g.
class="{required:true, messages:{required:'You must choose a site.'}}"`
but need to use the data attribute e.g.
data-validate="{required:true, messages:{required:'You must choose a site.'}}"`
The inputs may have more than one data attribute associated with it which won't be related to the validation e.g.
<input name="txt_txt001" type="text" maxlength="30" id="txt_txt001" class= "
{required:true, messages:{required:'This field is required.'} }" data-
children="ddl_txt007,ddl_txt008" data-optionGroup="1" data-optionGroupParent="" />
I know the ketchup plugin offers this but dont want to move over to it as I've put loads of work into getting the page setup with JQuery Validate.
Thanks
jQuery attr() Method attr('name','value'); First of all, specify a selector to get the reference of an element and call attr() method with attribute name parameter. To set the value of an attribute, pass value parameter along with name parameter. In the above example, $('p').
fn , so simply check whether it exists or not; if (typeof jQuery. fn. validationPlugin === "function") { // It's loaded } else { // It's not. }
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
Try making the following chages to validate 1.9.0. I've made a few mods so my line numbers may be off, but here goes:
Around ln 149:
var data = $.validator.normalizeRules(
$.extend(
{},
$.validator.metadataRules(element),
$.validator.classRules(element),
$.validator.dataRules(element), // add this
$.validator.attributeRules(element),
$.validator.staticRules(element)
), element);
Add this after classRules around ln 782:
// pretty much steal everything from the class based settings
dataRules: function(element) {
var rules = {};
var classes = $(element).data("validation");
classes && $.each(classes.split(' '), function() {
if (this in $.validator.classRuleSettings) {
$.extend(rules, $.validator.classRuleSettings[this]);
}
});
return rules;
},
Add any additional validators to the class rules:
jQuery.validator.addClassRules({
phone: {
phoneUS: true
},
zipcode: {
zipcode: true
},
notFirstSelect: {
notFirstSelect : true
}
});
Then add a data-validation
attribute to your for fields:
<input type="text" data-validation="zipcode required" maxlength="10" class="inputText med" value="" name="zip" id="zip">
This has been working really well for me. Check out: http://www.roomandboard.com/rnb/business_interiors/contactus/send.do for an example of this in use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With