jquery Validation Verify ID number, passport, phone number, email (instance code) _jquery

Source: Internet
Author: User
Tags valid email address

Validata.htm

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<!--templatebegineditable name= "DocTitle"-->
<title> Untitled Document </title>
<script src= "Lib/jquery.js" type= "Text/javascript" ></script>
<script src= "Lib/jquery.validate.js" type= "Text/javascript" ></script>
<script src= "Lib/card.js" type= "Text/javascript" ></script>
<script src= "Lib/validata.js" type= "Text/javascript" ></script>
<style type= "Text/css" >
em.success{
Background:url ("Images/tips_arrow.gif") no-repeat left 0px;
padding-left:16px;
margin-left:2px;
}
em.error{
Background:url ("Images/tips_arrow.gif") no-repeat left-51px;
Display:inline;
padding-left:10px;
Font-style:normal;
font-size:11px;
margin-left:2px;
font-family:12px/162% Arial, Helvetica, Sans-serif;

}
</style>

<body>
<form class= "Cmsform" id= "Commentform" method= "Get" action= "" >
<p>
<label for= "Cusername" > Name </label><em>*</em>
<input id= "Cusername" name= "username" size= "

</p>
<p>
<label for= "Cemail" > Email </label><em>*</em>
<input id= "Cemail" name= "email" size= "/>"

</p>
<p>
<label for= "card" > ID number </label><em>*</em>
<input id= "card" name= "card" size= "/>"

</p>
<p>
<label for= "Passport" > Passport number </label><em>*</em>
<input id= "Passport" name= "passport" size= "/>"

</p>
<p>
<label for= "Phone" > Phone number </label><em>*</em>
<input id= "Phone" name= "phone" size= "/>"

</p>
</form>
</body>


Validata.js
Copy Code code as follows:

$ (function () {

$.validator.setdefaults ({
Submithandler:function (form) {
Form.submit ();
}
});
Character validation
JQuery.validator.addMethod ("Stringcheck", function (value, Element) {
return this.optional (Element) | | /^[\u0391-\uffe5\w]+$/.test (value);
"Can only include Chinese characters, English letters, numbers and underscores");
Two bytes in text
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]);
"Make sure that the value entered is between 3-15 bytes (2 bytes in one)");

ID Card 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");

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 = = && Mobile.test (value));
"Please fill in 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 fill in your phone number correctly");

Contact Phone (cell phone/Phone can be) verified
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 fill in your contact number correctly";

ZIP Code Verification
JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
"Please fill in your zip code correctly");

Start validation
$ (' #commentForm '). Validate ({

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

},
passport:{
Required:true,
Passport:true

}
},


messages:{
Username: {
Required: "Please fill in User name",
Stringcheck: "User name can only include Chinese characters, English letters, numbers and underscores",
Byterangelength: "Username must be between 3-15 characters (2 characters in one)"
},
email:{
Required: "<font color=red> Please enter an email address </fond>",
Email: "Please enter a valid email address"
},
phone:{
Required: "Please enter your contact number",
Isphone: "Please enter a valid contact number"
},
address:{
Required: "Please enter your contact address",
Stringcheck: "Please enter your contact address correctly",
Byterangelength: Please detail your contact address for us to contact you.
},
card:{
Required: "Please enter your ID number",
Isidcardno: "Please enter the correct ID number"
},
passport:{
Required: "Please enter your Passport number",
Passport: "Please enter the correct passport number"
}
},


Focusinvalid:false,
Onkeyup:false,


Errorplacement:function (Error, Element) {
Error.appendto (Element.parent ());
},
Errorelement: "em",
Error:function (label) {Label.text (""). AddClass ("error");
});
})

Card.js
Copy Code code as follows:

var idcardnoutil = {

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: "Macau", 91: "Abroad"},


Powers: ["7", "9", "10", "" 5 "," 8 "," 4 "," 2 "," 1 "," 6 "," 3 "," 7 "," 9 "," 10 "," 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 > New Date ()) {
Return false;//birthday cannot be greater than the current date
}else if ((xdata.getfullyear () = = yyyy) && (xdata.getmonth () = = mm-1) && (xdata.getdate () = = dd) ){
return true;
}else{
return false;
}
},


Getparitybit:function (Idcardno) {
var id17 = idcardno.substring (0,17);

var power = 0;
for (Var i=0;i<17;i++) {
Power + + parseint (Id17.charat (i), ten) * parseint (idcardnoutil.powers[i));
}

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) {
Basic checksum for 15-bit and 18-bit ID numbers
var check =/^\d{15}| (\d{17} (\d|x| X)) $/.test (Idcardno);
if (!check) return false;
15-bit or 18-bit length of judgment
if (idcardno.length==15) {
Return Idcardnoutil.check15idcardno (Idcardno);
}else if (idcardno.length==18) {
Return Idcardnoutil.check18idcardno (Idcardno);
}else{
return false;
}
},

//Verify 15-bit ID number
Check15idcardno:function (idcardno) {
//15-bit identification number basic CHECKSUM
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;
//Checksum address code
var addresscode = idcardno.substring (0,6);
Check = Idcardnoutil.checkaddresscode (Addresscode);
if (!check) return false;
var birdaycode = ' n ' + idcardno.substring (6,12);
//Checksum date code
return Idcardnoutil.checkbirthdaycode (Birdaycode);
},

Verify the 18-digit ID number
Check18idcardno:function (Idcardno) {
Basic format check of 18-bit ID number
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 the School Check code
Return Idcardnoutil.checkparitybit (Idcardno);
},

Formatedatecn:function (Day) {
var yyyy =day.substring (0,4);
var mm = day.substring (4,6);
var dd = day.substring (6);
return yyyy + '-' + mm + '-' + dd;
},

Get information
Getidcardinfo:function (Idcardno) {
var idcardinfo = {
Gender: "",//gender
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 (14))%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 (16))%2==0) {
Idcardinfo.gender=idcardnoutil.genders.female;
}else{
Idcardinfo.gender=idcardnoutil.genders.male;
}

}
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) + ' + ' + idcardno.substring (6);
var paritybit = idcardnoutil.getparitybit (ID17);
return id17 + paritybit;
}else if (idcardno.length==18) {
return idcardno;
}else{
return null;
}
}
};
Verify that your passport is correct
function CheckNumber (number) {
var Str=number;
In JavaScript, regular expressions can only start and end with "/" and cannot use double quotes
var expression=/(p\d{7}) | (g\d{8})/;
var objexp=new RegExp (Expression);
if (Objexp.test (str) ==true) {
return true;
}else{
return 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.