var idCardNoUtil = { /*省,直辖市代码表*/ provinceAndCitys: {:"北京",:"天津",:"河北",:"山西",:"内蒙古",:"辽宁",:"吉林",:"黑龙江", :"上海",:"江苏",:"浙江",:"安徽",:"福建",:"江西",:"山东",:"河南",:"湖北",:"湖南",:"广东", :"广西",:"海南",:"重庆",:"四川",:"贵州",:"云南",:"西藏",:"陕西",:"甘肃",:"青海",:"宁夏", :"新疆",:"台湾",:"香港",:"澳门",:"国外"}, /*每位加权因子*/ powers: ["","","","","","","","","","","","","","","","",""], /*第位校检码*/ parityBit: ["","","X","","","","","","","",""], /*性别*/ genders: {male:"男",female:"女"}, /*校验地址码*/ checkAddressCode: function(addressCode){ var check = /^[-]\d{}$/.test(addressCode); if(!check) return false; if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(,))]){ return true; }else{ return false; } }, /*校验日期码*/ checkBirthDayCode: function(birDayCode){ var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))$/.test(birDayCode); if(!check) return false; var yyyy = parseInt(birDayCode.substring(,),); var mm = parseInt(birDayCode.substring(,),); var dd = parseInt(birDayCode.substring(),); var xdata = new Date(yyyy,mm-,dd); if(xdata > new Date()){ return false;//生日不能大于当前日期 }else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - ) && ( xdata.getDate() == dd ) ){ return true; }else{ return false; } }, /*计算校检码*/ getParityBit: function(idCardNo){ var id = idCardNo.substring(,); /*加权 */ var power = ; for(var i=;i<;i++){ power += parseInt(id.charAt(i),) * parseInt(idCardNoUtil.powers[i]); } /*取模*/ var mod = power % ; 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){ //位和位身份证号码的基本校验 var check = /^\d{}|(\d{}(\d|x|X))$/.test(idCardNo); if(!check) return false; //判断长度为位或位 if(idCardNo.length==){ return idCardNoUtil.checkIdCardNo(idCardNo); }else if(idCardNo.length==){ return idCardNoUtil.checkIdCardNo(idCardNo); }else{ return false; } }, //校验位的身份证号码 checkIdCardNo: function(idCardNo){ //位身份证号码的基本校验 var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}$/.test(idCardNo); if(!check) return false; //校验地址码 var addressCode = idCardNo.substring(,); check = idCardNoUtil.checkAddressCode(addressCode); if(!check) return false; var birDayCode = '' + idCardNo.substring(,); //校验日期码 return idCardNoUtil.checkBirthDayCode(birDayCode); }, //校验位的身份证号码 checkIdCardNo: function(idCardNo){ //位身份证号码的基本格式校验 var check = /^[-]\d{}[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}(\d|x|X)$/.test(idCardNo); if(!check) return false; //校验地址码 var addressCode = idCardNo.substring(,); check = idCardNoUtil.checkAddressCode(addressCode); if(!check) return false; //校验日期码 var birDayCode = idCardNo.substring(,); check = idCardNoUtil.checkBirthDayCode(birDayCode); if(!check) return false; //验证校检码 return idCardNoUtil.checkParityBit(idCardNo); }, formateDateCN: function(day){ var yyyy =day.substring(,); var mm = day.substring(,); var dd = day.substring(); return yyyy + '-' + mm +'-' + dd; }, //获取信息 getIdCardInfo: function(idCardNo){ var idCardInfo = { gender:"", //性别 birthday:"" // 出生日期(yyyy-mm-dd) }; if(idCardNo.length==){ var aday = '' + idCardNo.substring(,); idCardInfo.birthday=idCardNoUtil.formateDateCN(aday); if(parseInt(idCardNo.charAt())%==){ idCardInfo.gender=idCardNoUtil.genders.female; }else{ idCardInfo.gender=idCardNoUtil.genders.male; } }else if(idCardNo.length==){ var aday = idCardNo.substring(,); idCardInfo.birthday=idCardNoUtil.formateDateCN(aday); if(parseInt(idCardNo.charAt())%==){ idCardInfo.gender=idCardNoUtil.genders.female; }else{ idCardInfo.gender=idCardNoUtil.genders.male; } } return idCardInfo; }, /*位转位*/ getId: function(idCardNo){ if(idCardNo.length==){ return idCardNo; }else if(idCardNo.length==){ return idCardNo.substring(,) + idCardNo.substring(,); }else{ return null; } }, /*位转位*/ getId: function(idCardNo){ if(idCardNo.length==){ var id = idCardNo.substring(,) + '' + idCardNo.substring(); var parityBit = idCardNoUtil.getParityBit(id); return id + parityBit; }else if(idCardNo.length==){ return idCardNo; }else{ return null; } } }; //身份证号码验证 jQuery.validator.addMethod("idCardNo", function(value, element) { return this.optional(element) || idCardNoUtil.checkIdCardNo(value); }, "Please specify a valid ID number."); //获取身份证信息 var idCardInfo = idCardNoUtil.getIdCardInfo(idCardNo); alert(idCardInfo.gender + "|" + idCardInfo.birthday);
注:录入并判断数据库中是否已存在同样的身份证时
(1) 若输入的是15位的身份证:先查找15位的ID是否存在,若不存在还需要将15位的身份证转成18位的身份证,仍不存在的话才可录入系统。
(2) 若输入的是18位的身份证:先查找18位的ID是否存在,若不存在还需要将18位的身份证转成15位的身份证,仍不存在的话才可录入系统。
如果找到对应的15位身份证,需要将15位的更新到18位。
您可能感兴趣的文章: