Common verification rules
// Mobile phone number verification
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
JQuery. validator. addMethod ("chrnum", function (value, element ){ Var chrnum =/^ ([a-zA-Z0-9] +) $ /; Return this. optional (element) | (chrnum. test (value )); }, "Only numbers and letters can be entered (character A-Z, a-z, 0-9 )"); |
// Chinese verification
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
$. Validator. addMethod ("selectNone", function (value, element ){ Return value = "select "; }, "Must be selected "); |
// Bytes length verification
The code is as follows: |
Copy code |
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 ("Please ensure that the input value is between {0}-{1} bytes (one text is counted as two bytes )")); |
// Character verification
The code is as follows: |
Copy code |
JQuery. validator. addMethod ("stringCheck", function (value, element ){ Return this. optional (element) |/^ [u0391-uFFE5w] + $/. test (value ); }, "Can only contain Chinese characters, English letters, numbers, and underscores "); |
// Two Chinese characters
The code is as follows: |
Copy code |
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]); }, "Please make sure that the input value is between 3-15 bytes (one text is counted as 2 bytes )"); |