This is a pretty good link about Jquery validation:
http://www.javascript-coder.com/form-validation/jquery-form-validation-guide.phtml#rules_dynamic
Add a rule dynamically:
Remove a rule:
Create a new rule: (For example, password policy that uses regular expression for validation)
1. Inside $(document).ready(), add following line of code:
$.validator.addMethod(
"passwordpolicy",
function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Password must contain at least one letter and one digit"
);
2. In the HTML markup where you need to use this validation rule, add data-rule attribute data-rule-passwordpolicy and provide the regular expression as the value. The passwordpolicy
is the first parameter in the $.validator.addMethod() method.
<input id="tbxPassword" name="tbxPassword" type="password" class="form-control" data-rule-required="true" data-rule-passwordpolicy="^(?=.*[a-zA-Z])(?=.*[0-9]).{8,20}$" data-rule-minlength="8" data-rule-maxlength="20" data-bind="value: Password" />
Note: before adding or removing rule, you may need to call $("form").validate() first. Otherwise, you may get a TypeError error message.
Also this link talks about using Jquery validation with HTML data attribute. It is pretty neat solution for validation:
http://johnnycode.com/2014/03/27/using-jquery-validate-plugin-html5-data-attribute-rules/
Valid rule list:
http://www.javascript-coder.com/form-validation/jquery-form-validation-guide.phtml#rules_dynamic
Add a rule dynamically:
$(selector).rules(
'add'
,{max:50});
Remove a rule:
$(selector).rules(
'remove'
,'max');
Create a new rule: (For example, password policy that uses regular expression for validation)
1. Inside $(document).ready(), add following line of code:
$.validator.addMethod(
"passwordpolicy",
function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Password must contain at least one letter and one digit"
);
2. In the HTML markup where you need to use this validation rule, add data-rule attribute data-rule-passwordpolicy and provide the regular expression as the value. The passwordpolicy
is the first parameter in the $.validator.addMethod() method.
<input id="tbxPassword" name="tbxPassword" type="password" class="form-control" data-rule-required="true" data-rule-passwordpolicy="^(?=.*[a-zA-Z])(?=.*[0-9]).{8,20}$" data-rule-minlength="8" data-rule-maxlength="20" data-bind="value: Password" />
Note: before adding or removing rule, you may need to call $("form").validate() first. Otherwise, you may get a TypeError error message.
Also this link talks about using Jquery validation with HTML data attribute. It is pretty neat solution for validation:
http://johnnycode.com/2014/03/27/using-jquery-validate-plugin-html5-data-attribute-rules/
Valid rule list:
- data-rule-required="true"
- data-rule-email="true"
- data-rule-url="true"
- data-rule-date="true"
- data-rule-dateISO="true"
- data-rule-number="true"
- data-rule-digits="true"
- data-rule-creditcard="true"
- data-rule-minlength="6"
- data-rule-maxlength="24"
- data-rule-rangelength="5,10"
- data-rule-min="5"
- data-rule-max="10"
- data-rule-range="5,10"
- data-rule-equalto="#password"
- data-rule-remote="custom-validatation-endpoint.aspx"
- data-rule-accept=""
- data-rule-bankaccountNL="true"
- data-rule-bankorgiroaccountNL="true"
- data-rule-bic=""
- data-rule-cifES=""
- data-rule-creditcardtypes=""
- data-rule-currency=""
- data-rule-dateITA=""
- data-rule-dateNL=""
- data-rule-extension=""
- data-rule-giroaccountNL=""
- data-rule-iban=""
- data-rule-integer="true"
- data-rule-ipv4="true"
- data-rule-ipv6="true"
- data-rule-mobileNL=""
- data-rule-mobileUK=""
- data-rule-lettersonly="true"
- data-rule-nieES=""
- data-rule-nifES=""
- data-rule-nowhitespace="true"
data-rule-pattern=""(does not work)- data-rule-phoneNL="true"
- data-rule-phoneUK="true"
- data-rule-phoneUS="true"
- data-rule-phonesUK="true"
- data-rule-postalcodeNL="true"
- data-rule-postcodeUK="true"
- data-rule-require_from_group=""
- data-rule-skip_or_fill_minimum=""
- data-rule-strippedminlength=""
- data-rule-time=""
- data-rule-time12h=""
- data-rule-url2=""
- data-rule-vinUS=""
- data-rule-zipcodeUS="true"
- data-rule-ziprange=""
data-rule-equalto will compare the value in one input to another. The value of this attribute is the HTML element selector. For example, you can use #password to specify to compare against the input with id equals to 'password'. However, when the id contains square bracket, e.g., choices[0].ChoiceValue, the data-rule-equalto will not work if you use:
data-rule-equalto="#choices[0].ChoiceValue".
You can use different selector to achive the goal:
data-rule-equalto="input[id='choices[0].ChoiceValue']".
Comments