idCard.js閮ㄥ垎*******************************************************
//****************************************************************************
// 鏋勯犲嚱鏁幫紝鍙橀噺涓?5浣嶆垨鑰?8浣嶇殑韜喚璇佸彿鐮?br />function clsIDCard(CardNo) {
this.Valid=false;
this.ID15='';
this.ID18='';
this.Local='';
if(CardNo!=null)this.SetCardNo(CardNo);
}
// 璁劇疆韜喚璇佸彿鐮侊紝15浣嶆垨鑰?8浣?br />clsIDCard.prototype.SetCardNo = function(CardNo) {
this.ID15='';
this.ID18='';
this.Local='';
CardNo=CardNo.replace(" ","");
var strCardNo;
if(CardNo.length==18) {
pattern= /^\d{17}(\d|x|X)$/;
if (pattern.exec(CardNo)==null)return;
strCardNo=CardNo.toUpperCase();
} else {
pattern= /^\d{15}$/;
if (pattern.exec(CardNo)==null)return;
strCardNo=CardNo.substr(0,6)+'19'+CardNo.substr(6,9)
strCardNo+=this.GetVCode(strCardNo);
}
this.Valid=this.CheckValid(strCardNo);
}
// 鏍¢獙韜喚璇佹湁鏁堟?br />clsIDCard.prototype.IsValid = function() {
return this.Valid;
}
// 榪斿洖鐢熸棩瀛楃涓詫紝鏍煎紡濡備笅錛?981-10-10
clsIDCard.prototype.GetBirthDate = function() {
var BirthDate='';
if(this.Valid)BirthDate=this.GetBirthYear()+'-'+this.GetBirthMonth()+'-'+this.GetBirthDay();
return BirthDate;
}
// 榪斿洖鐢熸棩涓殑騫達紝鏍煎紡濡備笅錛?981
clsIDCard.prototype.GetBirthYear = function() {
var BirthYear='';
if(this.Valid)BirthYear=this.ID18.substr(6,4);
return BirthYear;
}
// 榪斿洖鐢熸棩涓殑鏈堬紝鏍煎紡濡備笅錛?0
clsIDCard.prototype.GetBirthMonth = function() {
var BirthMonth='';
if(this.Valid)BirthMonth=this.ID18.substr(10,2);
if(BirthMonth.charAt(0)=='0')BirthMonth=BirthMonth.charAt(1);
return BirthMonth;
}
// 榪斿洖鐢熸棩涓殑鏃ワ紝鏍煎紡濡備笅錛?0
clsIDCard.prototype.GetBirthDay = function() {
var BirthDay='';
if(this.Valid)BirthDay=this.ID18.substr(12,2);
return BirthDay;
}
// 榪斿洖鎬у埆錛?錛氱敺錛?錛氬コ
clsIDCard.prototype.GetSex = function() {
var Sex='';
if(this.Valid)Sex=this.ID18.charAt(16)%2;
return Sex;
}
// 榪斿洖15浣嶈韓浠借瘉鍙風爜
clsIDCard.prototype.Get15 = function() {
var ID15='';
if(this.Valid)ID15=this.ID15;
return ID15;
}
// 榪斿洖18浣嶈韓浠借瘉鍙風爜
clsIDCard.prototype.Get18 = function() {
var ID18='';
if(this.Valid)ID18=this.ID18;
return ID18;
}
// 榪斿洖鎵鍦ㄧ渷錛屼緥濡傦細涓婃搗甯傘佹禉姹熺渷
clsIDCard.prototype.GetLocal = function() {
var Local='';
if(this.Valid)Local=this.Local;
return Local;
}
clsIDCard.prototype.GetVCode = function(CardNo17) {
var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var Ai = new Array('1','0','X','9','8','7','6','5','4','3','2');
var cardNoSum = 0;
for (var i=0; i<CardNo17.length; i++)cardNoSum+=CardNo17.charAt(i)*Wi[i];
var seq = cardNoSum%11;
return Ai[seq];
}
clsIDCard.prototype.CheckValid = function(CardNo18) {
if(this.GetVCode(CardNo18.substr(0,17))!=CardNo18.charAt(17))return false;
if(!this.IsDate(CardNo18.substr(6,8)))return false;
var aCity={11:"鍖椾含",12:"澶╂觸",13:"娌沖寳",14:"灞辮タ",15:"鍐呰挋鍙?,21:"杈藉畞",22:"鍚夋灄",23:"榛戦緳姹?",31:"涓婃搗",32:"姹熻嫃",33:"嫻欐睙",34:"瀹夊窘",35:"紱忓緩",36:"姹熻タ",37:"灞變笢",41:"娌沖崡",42:"婀栧寳 ",43:"婀栧崡",44:"騫夸笢",45:"騫胯タ",46:"嫻峰崡",50:"閲嶅簡",51:"鍥涘窛",52:"璐靛窞",53:"浜戝崡",54:"瑗胯棌 ",61:"闄曡タ",62:"鐢樿們",63:"闈掓搗",64:"瀹佸",65:"鏂扮枂",71:"鍙版咕",81:"棣欐腐",82:"婢抽棬",91:"鍥藉"};
if(aCity[parseInt(CardNo18.substr(0,2))]==null)return false;
this.ID18=CardNo18;
this.ID15=CardNo18.substr(0,6)+CardNo18.substr(8,9);
this.Local=aCity[parseInt(CardNo18.substr(0,2))];
return true;
}
clsIDCard.prototype.IsDate = function(strDate) {
var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[2]-1, r[3]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[2]&&d.getDate()==r[3]);
}
欏甸潰閮ㄥ垎**************************************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>韜喚璇侀獙璇?lt;/title>
<script src="idCard.js"></script>
</head>
<body>
<script>
function valiIdCard(idCard){
var checkFlag = new clsIDCard(idCard);
if (!checkFlag.IsValid()) {
alert("杈撳叆鐨勮韓浠借瘉鍙鋒棤鏁?璇瘋緭鍏ョ湡瀹炵殑韜喚璇佸彿錛?);
document.getElementByIdx("idCard").focus();
return false;
}else{
alert("姝g‘錛?);
}
}
</script>
<input id="idCard" type="text" onblur="valiIdCard(this.value)"/>
</body>