JQuery. Validate usage notes (jQueryValidation example) _ jquery

Source: Internet
Author: User
Tags valid email address
After learning jQueryValidation, I wrote a public example and added it to my favorites for later use. The test code is included in the sample and needs to be tested by friends. Verify the operation class formValidatorClass. js

The Code is as follows:


/**
* @ Author ming
*/
$ (Document). ready (function (){
/** // * Set the default attribute */
$. Validator. setDefaults ({
SubmitHandler: function (form ){
Form. submit ();
}
});
// Character Verification
JQuery. validator. addMethod ("stringCheck", function (value, element ){
Return this. optional (element) |/^ [\ u0391-\ uFFE5 \ w] + $/. test (value );
}, "Can only contain Chinese characters, English letters, numbers, and underscores ");
// Two Chinese characters
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 )");
// ID card number verification
JQuery. validator. addMethod ("isIdCardNo", function (value, element ){
Return this. optional (element) | isIdCardNo (value );
}, "Enter your ID card number correctly ");
// Mobile phone number verification
JQuery. validator. addMethod ("isMobile", 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 ));
}, "Please enter your mobile phone number correctly ");
// Phone number verification
JQuery. validator. addMethod ("isTel", function (value, element ){
Var tel =/^ \ d {3, 4 }-? \ D {7, 9} $/; // Phone Number Format: 010-12345678
Return this. optional (element) | (tel. test (value ));
}, "Please enter your phone number correctly ");
// Contact number (mobile phone/phone) Verification
JQuery. validator. addMethod ("isPhone", function (value, element ){
Var length = value. length;
Var mobile =/^ (13 [0-9] {1}) | (15 [0-9] {1}) + \ d {8 }) $ /;
Var tel =/^ \ d {3, 4 }-? \ D {7, 9} $ /;
Return this. optional (element) | (tel. test (value) | mobile. test (value ));
}, "Please enter your contact number correctly ");
// Postal code verification
JQuery. validator. addMethod ("isZipCode", function (value, element ){
Var tel =/^ [0-9] {6} $ /;
Return this. optional (element) | (tel. test (value ));
}, "Please enter your zip code correctly ");
// Start Verification
$ ('# SubmitForm'). validate ({
/** // * Set verification rules */
Rules :{
Username :{
Required: true,
StringCheck: true,
ByteRangeLength: [3, 15]
},
Email :{
Required: true,
Email: true
},
Phone :{
Required: true,
IsPhone: true
},
Address :{
Required: true,
StringCheck: true,
ByteRangeLength: [3,100]
}
},
/** // * Set the error message */
Messages :{
Username :{
Required: "Enter the user name ",
StringCheck: "The user name can only contain Chinese characters, English letters, numbers, and underscores ",
ByteRangeLength: "The user name must be 3-15 characters long (one Chinese character is counted as 2 characters )"
},
Email :{
Required: "enter an Email address ",
Email: "enter a valid Email Address"
},
Phone :{
Required: "Enter your contact number ",
IsPhone: "enter a valid contact number"
},
Address :{
Required: "Enter your contact address ",
StringCheck: "Please enter your contact address correctly ",
ByteRangeLength: "Please provide your contact address so that we can contact you"
}
},
/** // * Set the verification trigger event */
FocusInvalid: false,
Onkeyup: false,
/** // * Set the error message prompt DOM */
ErrorPlacement: function (error, element ){
Error. appendTo (element. parent ());
},
});
});


Test page index.html

The Code is as follows:


Http://www.w3.org/TR/html4/loose.dtd>



JQuery Verification

Related Article

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.