Heim > php教程 > php手册 > js验证身份证类

js验证身份证类

WBOY
Freigeben: 2016-06-07 11:42:01
Original
1041 Leute haben es durchsucht

js验证身份证类
js验证身份证类var idCardNoUtil = {<br>     provinceAndCitys: {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>         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:"男",female:"女"},<br>         checkAddressCode: function(addressCode) {<br>             var check = /^[1-9]\d{5}$/.test(addressCode);<br>             if(!check) return false;<br>             if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0,2))]){<br>                 return true;<br>             } else {<br>                 return false;<br>             }<br>         },<br> <br>     checkBirthDayCode: function(birDayCode){<br>         var check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode);<br>         if(!check) return false;<br>         var yyyy = parseInt(birDayCode.substring(0,4),10);<br>         var mm = parseInt(birDayCode.substring(4,6),10);<br>         var dd = parseInt(birDayCode.substring(6),10);<br>         var xdata = new Date(yyyy,mm-1,dd);<br>         if(xdata > new Date()){<br>             return false;//生日不能大于当前日期<br>         }else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) ){<br>             return true;<br>         }else{<br>             return false;<br>         }<br>     },<br> <br>     getParityBit: function(idCardNo){<br>         var id17 = idCardNo.substring(0,17);<br>         var power = 0;<br>         for(var i=0;i             power += parseInt(id17.charAt(i),10) * parseInt(idCardNoUtil.powers[i]);<br>         }<br>         var mod = power % 11;<br>         return idCardNoUtil.parityBit[mod];<br>     },<br> <br>     checkParityBit: function(idCardNo){<br>         var parityBit = idCardNo.charAt(17).toUpperCase();<br>         if(idCardNoUtil.getParityBit(idCardNo) == parityBit){<br>             return true;<br>         }else{<br>             return false;<br>         }<br>     },<br> <br>     checkIdCardNo: function(idCardNo){<br>         //15位和18位身份证号码的基本校验<br>         var check = /^[0-9]{15}|([0-9]{17}([0-9]|x|X))$/.test(idCardNo);<br>         if(!check) return false;<br>         //判断长度为15位或18位<br>         if(idCardNo.length==15){<br>             return idCardNoUtil.check15IdCardNo(idCardNo);<br>         }else if(idCardNo.length==18){<br>             return idCardNoUtil.check18IdCardNo(idCardNo);<br>         }else{<br>             return false;<br>         }<br>     },<br>     //校验15位的身份证号码<br>     check15IdCardNo: function(idCardNo){<br>         //15位身份证号码的基本校验<br>         //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号<br>         var check = /^[1-9][0-9]{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))[0-9]{3}$/.test(idCardNo);<br>         if(!check) return false;<br>         //校验地址码<br>         var addressCode = idCardNo.substring(0,6);<br>         check = idCardNoUtil.checkAddressCode(addressCode);<br>         if(!check) return false;<br>             var birDayCode = '19' + idCardNo.substring(6,12);<br>         //校验日期码<br>         return idCardNoUtil.checkBirthDayCode(birDayCode);<br>     },<br>     //校验18位的身份证号码<br>     check18IdCardNo: function(idCardNo){<br>         //18位身份证号码的基本格式校验<br>         //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号<br>         var objReg = /^[1-9][0-9]{5}[1-9][0-9]{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))[0-9]{3}([0-9]|x|X)$/i;<br>         var check = objReg.test(idCardNo);<br>         if(!check) return false;<br>         //校验地址码<br>         var addressCode = idCardNo.substring(0,6);<br>         check = idCardNoUtil.checkAddressCode(addressCode);<br>         if(!check) return false;<br>         //校验日期码<br>         var birDayCode = idCardNo.substring(6,14);<br>         check = idCardNoUtil.checkBirthDayCode(birDayCode);<br>         if(!check) return false;<br>         //验证校检码<br>         return idCardNoUtil.checkParityBit(idCardNo);<br>     },<br>     formateDateCN: function(day){<br>         var yyyy =day.substring(0,4);<br>         var mm = day.substring(4,6);<br>         var dd = day.substring(6);<br>         return yyyy + '-' + mm +'-' + dd;<br>     },<br>     //获取信息<br>     getIdCardInfo: function(idCardNo){<br>         var idCardInfo = {<br>             gender:"", //性别<br>             birthday:"" // 出生日期(yyyy-mm-dd)<br>         };<br>         if(idCardNo.length==15){<br>             var aday = '19' + idCardNo.substring(6,12);<br>             idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);<br>             if(parseInt(idCardNo.charAt(14))%2==0){<br>                 idCardInfo.gender=idCardNoUtil.genders.female;<br>             }else{<br>                 idCardInfo.gender=idCardNoUtil.genders.male;<br>             }<br>         }else if(idCardNo.length==18){<br>             var aday = idCardNo.substring(6,14);<br>             idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);<br>             if(parseInt(idCardNo.charAt(16))%2==0){<br>                 idCardInfo.gender=idCardNoUtil.genders.female;<br>             }else{<br>                 idCardInfo.gender=idCardNoUtil.genders.male;<br>             }<br>         }<br>         return idCardInfo;<br>     },<br> <br>     getId15:function(idCardNo){<br>         if(idCardNo.length==15){<br>             return idCardNo;<br>         }else if(idCardNo.length==18){<br>             return idCardNo.substring(0,6) + idCardNo.substring(8,17);<br>         }else{<br>             return null;<br>         }<br>     },<br> <br>     getId18: function(idCardNo){<br>         if(idCardNo.length==15){<br>             var id17 = idCardNo.substring(0,6) + '19' + idCardNo.substring(6);<br>             var parityBit = idCardNoUtil.getParityBit(id17);<br>             return id17 + parityBit;<br>         }else if(idCardNo.length==18){<br>             return idCardNo;<br>         }else{<br>             return null;<br>         }<br>     }<br> };<br> //验证护照是否正确<br> function checknumber(number){<br>     var str=number;<br>     //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号<br>     var Expression=/(P\d{7})|(G\d{8})/;<br>     var objExp=new RegExp(Expression);<br>     if(objExp.test(str)==true){<br>        return true;<br>     }else{<br>        return false;<br>     } <br> };

AD:真正免费,域名+虚机+企业邮箱=0元

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage