jquery Plugin validation Implementation verification ID number, etc.

Source: Internet
Author: User
Tags expression valid valid email address zip

This article mainly introduces the jquery plug-in validation authentication ID number, passport, telephone number, email very simple and practical, the need for small partners can refer to.

First recommend a bootstrap jquery validation plugin: http://thrilleratplay.github.io/jquery-validation-bootstrap-tooltip/

JQuery Validation Add validation rules

Validata.html

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58-59 60 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131-132 $ (function () {&nbsp; $.validator.setdefaults ({submithandler:function (form) {Form.submit ();}});//character Validation Jquery.valida Tor.addmethod ("Stringcheck", function (value, Element) {return this.optional (element) | |/^[u0391-uffe5w]+$/.test ( Value); "Can only include Chinese characters, English letters, numbers and underscores"); Chinese text two bytes jQuery.validator.addMethod ("Byterangelength", function (value, element, param) {var length = value.length; for (var i = 0; i &lt; value.length; i++) {if (value.charcodeat (i) &gt; 127) {length++}} return this.optional (Element) | | (length &gt;= param[0] &amp;&amp; length &lt;= param[1]); "Make sure that the value entered is between 3-15 bytes (2 bytes in one)"); &nbsp;//ID number Verification JQuery.validator.addMethod ("Isidcardno", function (value, Element) {return this.optional (element) | | Idcardnoutil.checkidcardno (value); "Please enter your ID number correctly"); Passport number Verification JQuery.validator.addMethod ("Passport", function (value, Element) {return this.optional (element) | | CheckNumber (value); "Please enter your Passport number correctly"); &nbsp;//Mobile 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 = = &amp;&amp; Mobile.test (value)); "Please fill in your mobile phone number correctly"; &nbsp;//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 fill in your phone number correctly"); &nbsp;//Contact Phone (Mobile/Telephone) Verify 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)); &nbsp;}, "Please fill in your contact number correctly"); &nbsp;//ZIP code verification jQuery.validator.addMethod ("Iszipcode", function (value, Element) {var Tel =/^[0-9]{6}$/; return this.op tional (Element) | | (Tel.test (value)); "Please fill in your zip code correctly"); &nbsp;//Start verifying $ (' #commentForm '). Validate ({&nbsp; rules: {username: {required:true, stringcheck:true, Byterangelength: [3,15]}, email:{required:true, email:true}, phone:{required:true, ismobile:true}, address:{required:true, StringCheck:true, BYTERANGELENGTH:[3,100]}, card:{required:true, Isidcardno:true &nbsp;}, passport:{required:true, Passport:true &nbsp ; }, &nbsp; &nbsp; messages:{username: {required: "Please fill in User name", Stringcheck: "User name can only include Chinese characters, English letters, numbers and underscores", Byterangelength: "User name Must be between 3-15 characters (2 characters in a literal) "}, email:{required:" &lt;font color=red&gt; Please enter an email address &lt;/fond&gt; ", Email:" Please enter a valid email address "}, phone:{required:" Please enter your contact number ", Isphone:" Please enter a valid contact telephone "}, address:{required:" Please enter your contact address ", STRINGCHEC K: "Please enter your contact address correctly", Byterangelength: "Please detail your contact address for us to contact you"}, card:{required: "Please enter the ID number", Isidcardno: "Please enter the correct ID number"}, passport:{Required: "Please enter your Passport number", Passport: "Please enter the correct passport number"}, &nbsp; Focusinvalid:false, Onkeyup:false, Errorplacement:fun ction (Error, Element) {Error.appendto (element.parent ());}, Errorelement: "em", error:function (label) {Label.text ("") . addclass ("error");} }); })

Card.js

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162-163 var idcardnoutil = {&nbsp; Provinceandcitys: {11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"}, Powers: ["7", "9", "Ten", "5", "8", "4", "2", "1", "" 6 "," 3 "," 7 "," 9 "," Ten "," 5 "," 8 "," 4 "," 2 "], paritybit: [" 1 "," 0 "," X "," 9 "," 8 ", "7", "6", "5", "4", "3", "2"], genders: {male: "male", Female: "Female"}, Checkaddresscode:function (Addresscode) {var check =/^[1-9] D{5}$/.test (Addresscode); if (!check) return false; if (Idcardnoutil.provinceandcitys[parseint (addresscode.substring (0,2))] {return true;} else{return False}}, Checkbirthdaycode:function (Birdaycode) {var check =/^[1-9]d{3} ((0[1-9)) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1]) $/.test (Birdaycode); if (!check) return false; var yyyy = parseint (birdaycode.substring (0,4), 10); var mm = parseint (birdaycode.substring (4,6), 10); var dd = PArseint (birdaycode.substring (6), 10); var xdata = new Date (YYYY,MM-1,DD); if (XData &gt; New Date ()) {return false;//birthday cannot be greater than the current date}else if ((xdata.getfullyear () = = yyyy) &amp;&amp; (Xdata.getmont H () = = mm-1) &amp;&amp; (xdata.getdate () = = dd) {return true;} else{return false;}, Getparitybit:function (Idcardno) {var id17 = idcardno.substring (0,17); &nbsp; var power = 0; var i=0;i&lt;17;i++) {Power + + parseint (Id17.charat (i),) * parseint (Idcardnoutil.powers[i]);} &nbsp; var mod = power% 11; return Idcardnoutil.paritybit[mod]; }, Checkparitybit:function (Idcardno) {var paritybit = Idcardno.charat. toUpperCase (); if ( Idcardnoutil.getparitybit (idcardno) = = Paritybit) {return true;} else{return false;}, Checkidcardno:function (Idcardno) {//15-bit and 18-bit identity card number Basic checksum var check =/^d{15}| ( D{17} (d|x| X)) $/.test (Idcardno); if (!check) return false; Determine whether the length is 15-bit or 18-bit if (idcardno.length==15) {return Idcardnoutil.check15idcardno (Idcardno);} else if (idcardno.length==18) {return IdcardnouTil.check18idcardno (Idcardno); }else{return False}, &nbsp;//verify 15-bit ID number check15idcardno:function (idcardno) {//15-bit ID number for basic check var check =/^[1-9]d{7 } ((0[1-9]) | (1[0-2]) ((0[1-9]) | ([1-2][0-9]) | (3[0-1]) D{3}$/.test (Idcardno); if (!check) return false; Verify address code var Addresscode = idcardno.substring (0,6); Check = Idcardnoutil.checkaddresscode (Addresscode); if (!check) return false; var birdaycode = ' n ' + idcardno.substring (6,12); Check Date code return Idcardnoutil.checkbirthdaycode (Birdaycode); &nbsp;//Verify 18-bit ID number check18idcardno:function (idcardno) {//18-bit ID number basic format check var check =/^[1-9]d{5}[1-9]d{3} ((0[1-9) )| (1[0-2]) ((0[1-9]) | ([1-2][0-9]) | (3[0-1]) D{3} (d|x| X) $/.test (Idcardno); if (!check) return false; Verify address code var Addresscode = idcardno.substring (0,6); Check = Idcardnoutil.checkaddresscode (Addresscode); if (!check) return false; Check Date code var birdaycode = idcardno.substring (6,14); Check = Idcardnoutil.checkbirthdaycode (Birdaycode); if (!check) return false; Verify School Check code return Idcardnoutil.checkpariTybit (Idcardno); }, &nbsp; Formatedatecn:function (day) {var yyyy =day.substring (0,4); var mm = day.substring (4,6); var dd = day.substring ( 6); return yyyy + '-' + mm + '-' + dd; &nbsp;//Get information Getidcardinfo:function (Idcardno) {var idcardinfo = {gender: "",//Sex Birthday: ""//Birth date (YYYY-MM-DD)}; if (idcardno.length==15) {var aday = ' n ' + idcardno.substring (6,12); IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN ( Aday); if (parseint (Idcardno.charat)%2==0) {Idcardinfo.gender=idcardnoutil.genders.female;} else{Idcardinfo.gender=idcardnoutil.genders.male}} else if (idcardno.length==18) {var aday = idcardno.substring (6,14); IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN ( Aday); if (parseint (Idcardno.charat)%2==0) {Idcardinfo.gender=idcardnoutil.genders.female;} else{Idcardinfo.gender=idcardnoutil.genders.male} &nbsp;} return idcardinfo; }, Getid15:function (Idcardno) {if (idcardno.length==15) {return idcardno;} else if (idcardno.length==18) {return idcardno.substring (0,6) + Idcardno.SUBSTRING (8,17); }else{return null;}, Getid18:function (Idcardno) {if (idcardno.length==15) {var id17 = idcardno.substring (0,6) + ' 19 ' + Idcardno.substring (6); var paritybit = idcardnoutil.getparitybit (ID17); return id17 + paritybit; }else if (idcardno.length==18) {return idcardno;} else{return null;} }; Verify that the Passport is correct function checknumber (number) {var str=number;//in JavaScript, regular expressions can only use the "/" Start and end, and cannot use double quotes var expression=/(Pd {7}) | (gd{8})/; var objexp=new RegExp (Expression); if (Objexp.test (str) ==true) {return true;} else{return false;};

The above is the entire contents of this article, I hope you can enjoy.

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.