Add a custom Verification Method to the JQuery extension Validate 5

Source: Internet
Author: User

Example: Copy codeThe Code is as follows: <script type = "text/javascript">
JQuery. validator. addMethod ("regex", // addMethod 1st parameters: Method Name
Function (value, element, params) {// addMethod 2nd parameters: verification method, parameter (value of the verified element, verified element, parameter)
Var exp = new RegExp (params); // instantiate a regular object. The parameter is the input regular expression.
Return exp. test (value); // test whether a match exists.
},
"Format error"); // addMethod 3rd parameters: default error message
$ (Function (){
$ ("# SignupForm"). validate (
{
Rules :{
TxtPassword1: "required", // password 1 is required
TxtPassword2: {// The description of password 2 is more than one object type.
Required: true, // required. It can be an anonymous method.
Failed to: "# txtPassword1", // it must be the same as password 1
Rangelength: [5, 10], // Length 5-10
Regex: "^ \ w + $" // use a custom verification rule, which is added in the previous example.
},
TxtEmail: "email" // the email address must be valid.
},
Messages :{
TxtPassword1: "You must enter ",
TxtPassword2 :{
Required: "You must enter ",
Failed to: "inconsistent passwords ",
Rangelength: "The length must be a string between {0} and {1 ",
Regex: "passwords can only contain numbers, letters, and underscores"
}
},
Debug: false, // The form is not submitted if it is modified to true
SubmitHandler: function (){
Alert ("submitted ");
}
});
});
</Script>

Running result:

NOTE: If there are multiple parameters, you can use arrays, such as regex: [, 5]. You can use subscript to access params [0] in the method.
Some extension verification methods provided by netizens:Copy codeThe Code is as follows: // 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 = 11 & mobile. test (value ));
}, "Incorrect Mobile Phone Number Format ");
// 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 ));
}, "Incorrect Phone Number Format ");
// Postal code verification
JQuery. validator. addMethod ("zipCode", function (value, element ){
Var tel =/^ [0-9] {6} $ /;
Return this. optional (element) | (tel. test (value ));
}, "Incorrect zip code format ");
// QQ number verification
JQuery. validator. addMethod ("qq", function (value, element ){
Var tel =/^ [1-9] \ d {4, 9} $ /;
Return this. optional (element) | (tel. test (value ));
}, "Incorrect QQ number format ");
// 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 & RegExp. $4 <256 ));
}, "Ip address format error ");
// Letter and number verification
JQuery. validator. addMethod ("chrnum", function (value, element ){
Var chrnum =/^ ([a-zA-Z0-9] +) $ /;
Return this. optional (element) | (chrnum. test (value ));
}, "Only numbers and letters (character A-Z, a-z, 0-9 )");
// Chinese Verification
JQuery. validator. addMethod ("chinese", function (value, element ){
Var chinese =/^ [\ u4e00-\ u9fa5] + $ /;
Return this. optional (element) | (chinese. test (value ));
}, "Only Chinese characters can be entered ");
// Drop-down box Verification
$. Validator. addMethod ("selectNone", function (value, element ){
Return value = "select ";
}, "Must be selected ");
// Bytes length Verification
JQuery. validator. addMethod ("byteRangeLength", function (value, element, param ){
Var length = value. length;
For (var I = 0; I <value. length; I ++ ){
If (value. charCodeAt (I)> 127 ){
Length ++;
}
}
Return this. optional (element) | (length> = param [0] & length <= param [1]);
}, $. Validator. format ("make sure that the input value is between {0}-{1} bytes (two bytes for one text )"));

Validate_20110905.rar

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.