js验证身份证类

  1. var idCardNoUtil = {
  2.     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:"国外"},
  3.         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:"女"},
  4.         checkAddressCode: function(addressCode) {
  5.             var check = /^[1-9]\d{5}$/.test(addressCode);
  6.             if(!check) return false;
  7.             if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0,2))]){
  8.                 return true;
  9.             } else {
  10.                 return false;
  11.             }
  12.         },
  13.     checkBirthDayCode: function(birDayCode){
  14.         var check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode);
  15.         if(!check) return false;
  16.         var yyyy = parseInt(birDayCode.substring(0,4),10);
  17.         var mm = parseInt(birDayCode.substring(4,6),10);
  18.         var dd = parseInt(birDayCode.substring(6),10);
  19.         var xdata = new Date(yyyy,mm-1,dd);
  20.         if(xdata > new Date()){
  21.             return false;//生日不能大于当前日期
  22.         }else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) ){
  23.             return true;
  24.         }else{
  25.             return false;
  26.         }
  27.     },
  28.     getParityBit: function(idCardNo){
  29.         var id17 = idCardNo.substring(0,17);
  30.         var power = 0;
  31.         for(var i=0;i<17;i++){
  32.             power += parseInt(id17.charAt(i),10) * parseInt(idCardNoUtil.powers[i]);
  33.         }
  34.         var mod = power % 11;
  35.         return idCardNoUtil.parityBit[mod];
  36.     },
  37.     checkParityBit: function(idCardNo){
  38.         var parityBit = idCardNo.charAt(17).toUpperCase();
  39.         if(idCardNoUtil.getParityBit(idCardNo) == parityBit){
  40.             return true;
  41.         }else{
  42.             return false;
  43.         }
  44.     },
  45.     checkIdCardNo: function(idCardNo){
  46.         //15位和18位身份证号码的基本校验
  47.         var check = /^[0-9]{15}|([0-9]{17}([0-9]|x|X))$/.test(idCardNo);
  48.         if(!check) return false;
  49.         //判断长度为15位或18位
  50.         if(idCardNo.length==15){
  51.             return idCardNoUtil.check15IdCardNo(idCardNo);
  52.         }else if(idCardNo.length==18){
  53.             return idCardNoUtil.check18IdCardNo(idCardNo);
  54.         }else{
  55.             return false;
  56.         }
  57.     },
  58.     //校验15位的身份证号码
  59.     check15IdCardNo: function(idCardNo){
  60.         //15位身份证号码的基本校验
  61.         //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号
  62.         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);
  63.         if(!check) return false;
  64.         //校验地址码
  65.         var addressCode = idCardNo.substring(0,6);
  66.         check = idCardNoUtil.checkAddressCode(addressCode);
  67.         if(!check) return false;
  68.             var birDayCode = '19' + idCardNo.substring(6,12);
  69.         //校验日期码
  70.         return idCardNoUtil.checkBirthDayCode(birDayCode);
  71.     },
  72.     //校验18位的身份证号码
  73.     check18IdCardNo: function(idCardNo){
  74.         //18位身份证号码的基本格式校验
  75.         //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号
  76.         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;
  77.         var check = objReg.test(idCardNo);
  78.         if(!check) return false;
  79.         //校验地址码
  80.         var addressCode = idCardNo.substring(0,6);
  81.         check = idCardNoUtil.checkAddressCode(addressCode);
  82.         if(!check) return false;
  83.         //校验日期码
  84.         var birDayCode = idCardNo.substring(6,14);
  85.         check = idCardNoUtil.checkBirthDayCode(birDayCode);
  86.         if(!check) return false;
  87.         //验证校检码
  88.         return idCardNoUtil.checkParityBit(idCardNo);
  89.     },
  90.     formateDateCN: function(day){
  91.         var yyyy =day.substring(0,4);
  92.         var mm = day.substring(4,6);
  93.         var dd = day.substring(6);
  94.         return yyyy + '-' + mm +'-' + dd;
  95.     },
  96.     //获取信息
  97.     getIdCardInfo: function(idCardNo){
  98.         var idCardInfo = {
  99.             gender:"", //性别
  100.             birthday:"" // 出生日期(yyyy-mm-dd)
  101.         };
  102.         if(idCardNo.length==15){
  103.             var aday = '19' + idCardNo.substring(6,12);
  104.             idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
  105.             if(parseInt(idCardNo.charAt(14))%2==0){
  106.                 idCardInfo.gender=idCardNoUtil.genders.female;
  107.             }else{
  108.                 idCardInfo.gender=idCardNoUtil.genders.male;
  109.             }
  110.         }else if(idCardNo.length==18){
  111.             var aday = idCardNo.substring(6,14);
  112.             idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
  113.             if(parseInt(idCardNo.charAt(16))%2==0){
  114.                 idCardInfo.gender=idCardNoUtil.genders.female;
  115.             }else{
  116.                 idCardInfo.gender=idCardNoUtil.genders.male;
  117.             }
  118.         }
  119.         return idCardInfo;
  120.     },
  121.     getId15:function(idCardNo){
  122.         if(idCardNo.length==15){
  123.             return idCardNo;
  124.         }else if(idCardNo.length==18){
  125.             return idCardNo.substring(0,6) + idCardNo.substring(8,17);
  126.         }else{
  127.             return null;
  128.         }
  129.     },
  130.     getId18: function(idCardNo){
  131.         if(idCardNo.length==15){
  132.             var id17 = idCardNo.substring(0,6) + '19' + idCardNo.substring(6);
  133.             var parityBit = idCardNoUtil.getParityBit(id17);
  134.             return id17 + parityBit;
  135.         }else if(idCardNo.length==18){
  136.             return idCardNo;
  137.         }else{
  138.             return null;
  139.         }
  140.     }
  141. };
  142. //验证护照是否正确
  143. function checknumber(number){
  144.     var str=number;
  145.     //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号
  146.     var Expression=/(P\d{7})|(G\d{8})/;
  147.     var objExp=new RegExp(Expression);
  148.     if(objExp.test(str)==true){
  149.        return true;
  150.     }else{
  151.        return false;
  152.     } 
  153. };

 

身份证 /(^\d{15}$)|(^\d{17}([0-9]|X)$)/
  
  护照 /^[a-zA-Z0-9]{3,21}$/   /^(P\d{7})|(G\d{8})$/
  
  军官证或士兵证 /^[a-zA-Z0-9]{7,21}$/
  
  港澳回归证 和 台胞证/^[a-zA-Z0-9]{5,21}$/
  
  出生证 /^[a-zA-Z0-9]{5,21}$/
  
  户口本 /^[a-zA-Z0-9]{3,21}$/

posted on 2018-01-05 14:06  大西瓜3721  阅读(351)  评论(0编辑  收藏  举报

导航