`
ddsheng1128
  • 浏览: 22539 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

js 验证身份证号码

阅读更多

idCard.js部分*******************************************************


//****************************************************************************

// 构造函数,变量为15位或者18位的身份证号码
function clsIDCard(CardNo) {
<wbr>this.Valid=false;<br><wbr>this.ID15='';<br><wbr>this.ID18='';<br><wbr>this.Local='';<br><wbr>if(CardNo!=null)this.SetCardNo(CardNo);<br> }</wbr></wbr></wbr></wbr></wbr>

// 设置身份证号码,15位或者18位
clsIDCard.prototype.SetCardNo = function(CardNo) {
<wbr>this.ID15='';<br><wbr>this.ID18='';<br><wbr>this.Local='';<br><wbr>CardNo=CardNo.replace(" ","");<br><wbr>var strCardNo;<br><wbr>if(CardNo.length==18) {<br><wbr><wbr><wbr>pattern= /^\d{17}(\d|x|X)$/;<br><wbr><wbr><wbr>if (pattern.exec(CardNo)==null)return;<br><wbr><wbr><wbr>strCardNo=CardNo.toUpperCase();<br><wbr>} else {<br><wbr><wbr><wbr>pattern= /^\d{15}$/;<br><wbr><wbr><wbr>if (pattern.exec(CardNo)==null)return;<br><wbr><wbr><wbr>strCardNo=CardNo.substr(0,6)+'19'+CardNo.substr(6,9)<br><wbr><wbr><wbr>strCardNo+=this.GetVCode(strCardNo);<br><wbr>}<br><wbr>this.Valid=this.CheckValid(strCardNo);<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

// 校验身份证有效性
clsIDCard.prototype.IsValid = function() {
<wbr>return this.Valid;<br> }</wbr>

// 返回生日字符串,格式如下,1981-10-10
clsIDCard.prototype.GetBirthDate = function() {
<wbr>var BirthDate='';<br><wbr>if(this.Valid)BirthDate=this.GetBirthYear()+'-'+this.GetBirthMonth()+'-'+this.GetBirthDay();<br><wbr>return BirthDate;<br> }</wbr></wbr></wbr>

// 返回生日中的年,格式如下,1981
clsIDCard.prototype.GetBirthYear = function() {
<wbr>var BirthYear='';<br><wbr>if(this.Valid)BirthYear=this.ID18.substr(6,4);<br><wbr>return BirthYear;<br> }</wbr></wbr></wbr>

// 返回生日中的月,格式如下,10
clsIDCard.prototype.GetBirthMonth = function() {
<wbr>var BirthMonth='';<br><wbr>if(this.Valid)BirthMonth=this.ID18.substr(10,2);<br><wbr>if(BirthMonth.charAt(0)=='0')BirthMonth=BirthMonth.charAt(1);<br><wbr>return BirthMonth;<br> }</wbr></wbr></wbr></wbr>

// 返回生日中的日,格式如下,10
clsIDCard.prototype.GetBirthDay = function() {
<wbr>var BirthDay='';<br><wbr>if(this.Valid)BirthDay=this.ID18.substr(12,2);<br><wbr>return BirthDay;<br> }</wbr></wbr></wbr>

// 返回性别,1:男,0:女
clsIDCard.prototype.GetSex = function() {
<wbr>var Sex='';<br><wbr>if(this.Valid)Sex=this.ID18.charAt(16)%2;<br><wbr>return Sex;<br> }</wbr></wbr></wbr>

// 返回15位身份证号码
clsIDCard.prototype.Get15 = function() {
<wbr>var ID15='';<br><wbr>if(this.Valid)ID15=this.ID15;<br><wbr>return ID15;<br> }</wbr></wbr></wbr>

// 返回18位身份证号码
clsIDCard.prototype.Get18 = function() {
<wbr>var ID18='';<br><wbr>if(this.Valid)ID18=this.ID18;<br><wbr>return ID18;<br> }</wbr></wbr></wbr>

// 返回所在省,例如:上海市、浙江省
clsIDCard.prototype.GetLocal = function() {
<wbr>var Local='';<br><wbr>if(this.Valid)Local=this.Local;<br><wbr>return Local;<br> }</wbr></wbr></wbr>

clsIDCard.prototype.GetVCode = function(CardNo17) {
<wbr>var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);<br><wbr>var Ai = new Array('1','0','X','9','8','7','6','5','4','3','2');<br><wbr>var cardNoSum = 0;<br><wbr>for (var i=0; i&lt;CardNo17.length; i++)cardNoSum+=CardNo17.charAt(i)*Wi[i];<br><wbr>var seq = cardNoSum%11;<br><wbr>return Ai[seq];<br> }</wbr></wbr></wbr></wbr></wbr></wbr>

clsIDCard.prototype.CheckValid = function(CardNo18) {
<wbr>if(this.GetVCode(CardNo18.substr(0,17))!=CardNo18.charAt(17))return false;<br><wbr>if(!this.IsDate(CardNo18.substr(6,8)))return false;<br><wbr>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:"国外"};<br><wbr>if(aCity[parseInt(CardNo18.substr(0,2))]==null)return false;<br><wbr>this.ID18=CardNo18;<br><wbr>this.ID15=CardNo18.substr(0,6)+CardNo18.substr(8,9);<br><wbr>this.Local=aCity[parseInt(CardNo18.substr(0,2))];<br><wbr>return true;<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

clsIDCard.prototype.IsDate = function(strDate) {
<wbr>var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);<br><wbr>if(r==null)return false;<br><wbr>var d= new Date(r[1], r[2]-1, r[3]);<br><wbr>return (d.getFullYear()==r[1]&amp;&amp;(d.getMonth()+1)==r[2]&amp;&amp;d.getDate()==r[3]);<br> }</wbr></wbr></wbr></wbr>

<wbr></wbr>

页面部分**************************************************************

<!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>身份证验证</title>
<script src="idCard.js"></script>
</head>
<body>
<script>
<wbr>function valiIdCard(idCard){<br><wbr><wbr>var checkFlag = new clsIDCard(idCard);<br><wbr><wbr>if (!checkFlag.IsValid()) {<br><wbr><wbr><wbr>alert("输入的身份证号无效,请输入真实的身份证号!");<br><wbr><wbr><wbr>document.getElementByIdx("idCard").focus();<br><wbr><wbr><wbr>return false;<br><wbr><wbr>}else{<br><wbr><wbr><wbr><wbr>alert("正确!");<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

</script>
<input id="idCard" type="text" onblur="valiIdCard(this.value)"/>
</body>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics