Jquery.validate Custom Validation methods

Source: Internet
Author: User
Tags zip
$ (document). Ready (function () {/** * ID number Verification * */function Isidcardno (num) {var Factorarr = NewArray (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); var paritybit= NewArray ("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); var vararray = NewArray (); var intvalue; var lngproduct = 0; var intcheckdigit; var intstrlen = num.length; var idNumber = num; Initialize if((Intstrlen! =) && (Intstrlen! = 18)) { return false; }//Check and set value for(i=0;i<intstrlen;i++) {Vararray[i] = Idnumber.charat (i); if((Vararray[i] < ' 0 ' | | vararray[i] > ' 9 ') && (i! = 17)) { return false; } Else if(I < 17) {Vararray[i] = vararray[i] * factorarr[i];} } if(Intstrlen = = 18) {//check date var Date8 = idnumber.substring (6,14); if(IsDate8 (date8) = = false) { return false; }//Calculate the sum of the products for(i=0;i<17;i++) {lngproduct = lngproduct + vararray[i];} Calculate the check digit Intcheckdigit = paritybit[lngproduct% 11]; Check last digit if(vararray[17]! = intcheckdigit) { return false; } } Else{//length is//check date var date6 = idnumber.substring (6,12); if(IsDate6 (date6) = = false) { return false; } } return true; }/** * Determines if the period is "yyyymm" * */function IsDate6 (sdate) { if(!/^[0-9]{6}$/.test (Sdate)) { return false; } var year, month, day; Year = sdate.substring (0, 4); month = sdate.substring (4, 6); if(Year < 1700 | |-year > 2500) return false if(Month < 1 | | month > 12) return false return true}/** * Determines if the period is "YYYYMMDD" * */function IsDate8 (sdate) { if(!/^[0-9]{8}$/.test (Sdate)) { return false; } var year, month, day; Year = sdate.substring (0, 4); month = sdate.substring (4, 6); Day = sdate.substring (6, 8); var iamonthdays = [31,28,31,30,31,30,31,31,30,31,30,31] if(Year < 1700 | |-year > 2500) return false if(((year% 4 = = 0) && (year% 100! = 0)) | | (Year% 400 = = 0)) iamonthdays[1]=29; if(Month < 1 | | month > 12) return false if(Day < 1 | | day > IAMONTHDAYS[MONTH-1]) return false return true}//ID number Verification JQuery.validator.addMethod ("Idcardno", function (value, Element) { return This. Optional (Element) | | Isidcardno (value); }, "Please enter your ID number correctly"); Alpha-Numeric JQuery.validator.addMethod ("Alnum", function (value, Element) { return This. Optional (Element) | | /^[a-za-z0-9]+$/.test (value); }, "can only include English letters and Numbers"); ZIP Code verification JQuery.validator.addMethod ("ZipCode", function (value, Element) {var Tel =/^[0-9]{6}$/; return This. Optional (Element) | | (Tel.test (value)); }, "Please fill in the ZIP code correctly"); Kanji JQuery.validator.addMethod ("Chcharacter", function (value, Element) {var Tel =/^[\ a-\ calls]+$/; return This. Optional (Element) | | (Tel.test (value)); }, "Please enter Chinese characters"); Minimum character length verification (one Chinese characters length is 2) jQuery.validator.addMethod ("Stringminlength", 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); }, $.validator.format ("length cannot be less than {0}!")); Character Maximum length verification (one Chinese characters length is 2) jQuery.validator.addMethod ("Stringmaxlength", 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); }, $.validator.format ("length cannot be greater than {0}!")); Character Validation JQuery.validator.addMethod ("string", function (value, Element) { return This. Optional (Element) | | /^[\α-\¥\w]+$/.test (value); }, "Do not allow special symbols!"); Mobile number verification JQuery.validator.addMethod ("mobile", function (value, Element) {var length = Value.length; return This. Optional (Element) | | (length = = &&/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +\d{8}) $/.test (value)); }, "Phone number format is wrong!"); Phone number verification JQuery.validator.addMethod ("Phone", function (value, Element) {var Tel =/^ (\d{3,4}-?)? \d{7,9}$/g; return This. Optional (Element) | | (Tel.test (value)); }, "Phone number format is wrong!"); ZIP Code verification JQuery.validator.addMethod ("ZipCode", function (value, Element) {var Tel =/^[0-9]{6}$/; return This. Optional (Element) | | (Tel.test (value)); }, "bad postal code format!"); JQuery.validator.addMethod must be validated at the beginning of a particular string ("Begin", function (value, element, param) {var begin = NewRegExp ("^" + param); return This. Optional (Element) | | (Begin.test (value)); }, $.validator.format ("must start with {0}!")); Verify that the two-time input values are not the same jQuery.validator.addMethod ("Notequalto", function (value, element, param) { returnValue! = $ (param). Val (); }, $.validator.format ("two times input cannot be the same!")); The validation value is not allowed with a specific value equal to JQuery.validator.addMethod ("NotEqual", function (value, element, param) { returnValue! = param; }, $.validator.format ("input value not allowed for {0}!")); The validation value must be greater than a specific value (not equal to) JQuery.validator.addMethod ("GT", function (value, element, param) { returnValue > param; }, $.validator.format ("Input value must be greater than {0}!")); Verify that the number of decimal digits cannot exceed two bits JQuery.validator.addMethod ("decimal", function (value, Element) {var decimal =/^-?\d+ (\.\d{1,2})? $/; return This. Optional (Element) | | (Decimal.test (value)); Jquery.validate usage April 12, 2010 Monday 14:33 name return type description Validate (options) return: Validator Verify that the selected form valid () is returned: Boolean Check to verify through R    Ules () Return: Options return element Validation rule rules (Add,rules) Return: Options Add Validation Rule rules (Remove,rules) Jquery.validate is a very good validation framework based on jquery, which allows us to quickly validate some of the common inputs, and to expand our own validation methods, as well as to support internationalization. Jquery.validate Official Website: http://bassistance.de/jquery-plugins/jquery-plugin-validation/usage: 1, First download jquery.js and jquery.validate.js and introduce JS file (Note: jquery must be introduced before jquery.validate.js, otherwise it will be an error) <script type= "text/ JavaScript "src=" Jquery.js "></script> <script type=" Text/javascript "src=" Jquery.validate.js "></ Script> 2, write the form code that needs to be validated and write the verification code (there are two ways to write the validation code, first use the normal way) var validator = $ ("FormId"). Validate ({//#formId为需要进行验证的表单ID Errorelement: "div",//use "div" tag tag error, default: "Label" wrapper: "Li",//Use "Li" label and then wrap the top errorelement errorclass: " Validate-error ",//Error prompt CSS class name" Error "onsubmit: true,//Whether the commit is validation, default: True onfocusout: true,//whether to validate when getting focus, default: True onkeyup: true,//whether to verify when tapping the keyboard, default: True onclick: false,//whether to validate on mouse click (General authentication Checkbox,radiobox) Focuscleanup: false,

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.