Add a custom Verification Method to the jquery extension validate-5

Source: Internet
Author: User
The preceding example shows that the self-contained verification method in validate is sufficient to meet General requirements. For special requirements, you can use addmethod (name, method, message) to add custom verification rules, the following example adds an extension Verification Method for regular expression verification. The example 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 Matching
},
" Incorrect format " ); // Addmethod 3rd parameters: default error message
$ ( Function (){
$ ( " # Signupform " ). Validate (

{
Rules :{
Txtpassword1: " Required " , // Password 1 is required

Txtpassword2 :{ // The description of password 2 contains more than one object type.
Required: True , // Required. It can be an anonymous method.
Similar: " # Txtpassword1 " , // Must be equal to password 1
Rangelength :[ 5 , 10 ], // Length: 5-10
RegEx: " ^ \ W + $ " // Use a custom verification rule.
},

txtemail: " email " // the email address must be valid
},
messages: {
txtpassword1: " you must enter " ,

Txtpassword2 :{
Required: " You must enter " ,
Similar: " Inconsistent passwords " ,
Rangelength: " The string must be between {0} and {1 }. " ,
RegEx: " The password can only contain numbers, letters, and underscores. "
}
},
Debug: False , // If it is set to true, the form is not submitted.
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:
// 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 )"));

Source code download

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.