jquery extension Validate Add custom validation method

Source: Internet
Author: User
Tags zip

Validate validation methods are sufficient to meet general requirements, and for special requirements you can add custom validation rules using Addmethod (Name,method,message), and the following example adds a method of extended validation for regular expression validation. Examples are as follows:

<script type= "text/web Effects" >
JQuery.validator.addMethod ("Regex",//addmethod 1th parameter: Method name
function (value, element, params) {//addmethod 2nd parameter: Validation method, parameter (value of the validated element, validated element, parameter)
var exp = new RegExp (params); Instantiate the regular object, with the argument passed into the regular expression
return exp.test (value); Test matches
},
"Format error"); Addmethod 3rd parameter: Default error message
$ (function () {
$ ("#signupForm"). Validate (

{
Rules: {
TxtPassword1: "Required",//password 1 required

TxtPassword2: {//Password 2 description More than 1 items using object type
Required:true,//required, here can be an anonymous method
Equalto: "#txtPassword1",//must be equal to password 1
Rangelength: [5, 10],//Length 5-10
Regex: "^w+$"//using custom validation rules, the new section in the previous example
},

Txtemail: "Email"//e-mail must be legal
},
Messages: {
TxtPassword1: "You must fill in",

TxtPassword2: {
Required: "You must fill in",
Equalto: "Password Inconsistent",
Rangelength: "String length must be between {0} and {1}",
Regex: "Password can only be numbers, letters and underscores"
}
},
Debug:false,//if modified to true, the form will not be submitted
Submithandler:function () {
Alert ("Beginning of submission");
}
});
});
</script>
Run Result:
Note that if you have more than one parameter that can use an array, such as regex:[1,3,5], you can use subscript access in the method: Params[0]
Some extended authentication methods provided by users:
Mobile phone number Verification
JQuery.validator.addMethod ("mobile", function (value, Element) {
var length = Value.length;
var mobile =/^ ((13[0-9]{1}) | ( 15[0-9]{1}) +d{8}) $/
return this.optional (Element) | | (length = = && Mobile.test (value));
}, "mobile phone number format error");
Phone number Verification
JQuery.validator.addMethod ("Phone", function (value, Element) {
var tel =/^ (0[0-9]{2,3}-)? ([2-9][0-9]{6,7}) + (-[0-9]{1,4})? $/;
return this.optional (Element) | | (Tel.test (value));
}, "Phone number format error");
ZIP Code Verification
JQuery.validator.addMethod ("ZipCode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
}, "ZIP code format error");
QQ Number Verification
JQuery.validator.addMethod ("QQ", function (value, Element) {
var tel =/^[1-9]d{4,9}$/;
return this.optional (Element) | | (Tel.test (value));
}, "QQ number format error");
IP Address Verification
JQuery.validator.addMethod ("IP", function (value, Element) {
var ip =/^ (?:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?). {3} (?: 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) $/;
return this.optional (Element) | | (Ip.test (value) && (Regexp.$1 < 256 && regexp.$2 < 256 && regexp.$3 < 256 && Rege Xp.$4 < 256));
}, "IP address format error");
Verification of letters and numbers
JQuery.validator.addMethod ("Chrnum", function (value, Element) {
var chrnum =/^ ([a-za-z0-9]+) $/;
return this.optional (Element) | | (Chrnum.test (value));
"Only numbers and letters (characters A-Z, A-Z, 0-9)" can be entered;
Verification in Chinese
JQuery.validator.addMethod ("Chinese", function (value, Element) {
var Chinese =/^[u4e00-u9fa5]+$/;
return this.optional (Element) | | (Chinese.test (value));
"Can only be entered in Chinese");
Dropdown box Validation
$.validator.addmethod ("Selectnone", function (value, Element) {
return value = = "Please select";
"Must select an item");
//Byte length validation
JQuery.validator.addMethod ("Byterangelength", function (value, element, param) {
  & nbsp var length = Value.length;
    for (var i = 0; i < value.length i++) {
        if (v Alue.charcodeat (i) > 127) {
            length++;
       }
   }
    return this.optional (element) | | (length >= param[0] && length <= param[1]);
}, $.validator.format ("Make sure the value entered is between {0}-{1} bytes (2 bytes in one)");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.