`
ap061ap
  • 浏览: 13245 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

18位身份证验证代码

 
阅读更多

  首先php版本的: /** * 身份证 * * @param string $id * @return boolean */ function is_idcard( $id ) { $id = strtoupper($id); $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/"; $arr_split = array(); if(!preg_match($regx, $id)) { return FALSE; } if(15==strlen($id)) //检查15位 { $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/"; @preg_match($regx, $id, $arr_split); //检查生日日期是否正确 $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4]; if(!strtotime($dtm_birth)) { return FALSE; } else { return TRUE; } } else //检查18位 { $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X )$/"; @preg_match($regx, $id, $arr_split); $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4]; if(!strtotime($dtm_birth)) //检查生日日期是否正确 { return FALSE; } else { //检验18位身份证的校验码是否正确。 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $sign = 0; for ( $i = 0; $i  * 身份证号码结构: *  * 17位数字和1位校验码:6位地址码数字,8位生日数字,3位出生时间顺序码,1位校验码。 * 地址码(前6位):表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。 * 出生日期码(第七位至十四位):表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。 * 顺序码(第十五位至十七位) :表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号, * 顺序码的奇数分配给男性,偶数分配给女性。 * 校验码(第十八位数): *  * 十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, , 16 ,先对前17位数字的权求和; * Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2; * 计算模 Y = mod(S, 11) * 通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2 *  *  *  * * @author xylz * @since 2011-1-4 * @see {@link http://www.blogjava.net/zeroline/archive/2011/01/0 3/342227.html} */ public class IdcardUtil { final static Map zoneNum = new HashMap(); static { zoneNum.put(11, "北京"); zoneNum.put(12, "天津"); zoneNum.put(13, "河北"); zoneNum.put(14, "山西"); zoneNum.put(15, "内蒙古"); zoneNum.put(21, "辽宁"); zoneNum.put(22, "吉林"); zoneNum.put(23, "黑龙江"); zoneNum.put(31, "上海"); zoneNum.put(32, "江苏"); zoneNum.put(33, "浙江"); zoneNum.put(34, "安徽"); zoneNum.put(35, "福建"); zoneNum.put(36, "江西"); zoneNum.put(37, "山东"); zoneNum.put(41, "河南"); zoneNum.put(42, "湖北"); zoneNum.put(43, "湖南"); zoneNum.put(44, "广东"); zoneNum.put(45, "广西"); zoneNum.put(46, "海南"); zoneNum.put(50, "重庆"); zoneNum.put(51, "四川"); zoneNum.put(52, "贵州"); zoneNum.put(53, "云南"); zoneNum.put(54, "西藏"); zoneNum.put(61, "陕西"); zoneNum.put(62, "甘肃"); zoneNum.put(63, "青海"); zoneNum.put(64, "宁夏"); zoneNum.put(65, "新疆"); zoneNum.put(71, "台湾"); zoneNum.put(81, "香港"); zoneNum.put(82, "澳门"); zoneNum.put(91, "国外"); } final static int[] PARITYBIT = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; final static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; /** * 身份证号是否基本有效 * * @param s * 号码内容 * @return 是否有效,null和""都是false */ public static boolean isIdcard(String s) { if (s == null || (s.length() != 15 && s.length() != 18)) return false; final char[] cs = s.toUpperCase().toCharArray(); // (1)校验位数 int power = 0; for (int i = 0; i  '9') return false; if (i  Calendar.getInstance().get(Calendar.YEAR)) { return false;// 1900年的PASS,超过今年的PASS } // (4)校验月份 String month = s.length() == 15 ? s.substring(8, 10) : s.substring(10, 12); final int imonth = Integer.parseInt(month); if (imonth  12) return false; // (5)校验天数 String day = s.length() == 15 ? s.substring(10, 12) : s.substring(12, 14); final int iday = Integer.parseInt(day); if (iday  31) return false; // (6)校验一个合法的年月日 if (!validate(iyear, imonth, iday)) return false; // (7)校验"校验码" if (s.length() == 15) return true; return cs[cs.length - 1] == PARITYBIT[power % 11]; } static boolean validate(int year, int month, int day) { //比如考虑闰月,大小月等 return true; } public static void main(String[] args) { for(int i=0;i "+isIdcard(s)); } } }  接着js版本的: function nunber(){ var idcard=document.getElementById('u_nunber').value; var Errors=new Array("验证通过!","身份证号码位数不对!","出生日期超出范围或含有非法字符!","身份证号码校验错误!","身份证地区非法!"); var area={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:"国外"} var idcard,Y,JYM; var S,M; var idcard_array = new Array(); idcard_array = idcard.split(""); if(area[parseInt(idcard.substr(0,2))]==null) { document.getElementById('nunber_re').innerHTML=""+Errors[4]+""; return false; } switch(idcard.length){ case 15: if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){ ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12 )(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1 -2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; } else { ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12 )(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1 -2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; } if(ereg.test(idcard)){ document.getElementById('nunber_re').innerHTML=""+Errors[0]+""; return true; } else { document.getElementById('nunber_re').innerHTML=""+Errors[2]+""; return false; } break; case 18: //18位身份号码检测 if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){ ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10| 12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]| [1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx ]$/; } else { ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10| 12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]| [1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0- 9Xx]$/; } if(ereg.test(idcard)){ S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ; Y = S % 11; M = "F"; JYM = "10X98765432"; M = JYM.substr(Y,1); if(M == idcard_array[17]){ document.getElementById('nunber_re').innerHTML=""+Errors[0]+""; return true; } else { document.getElementById('nunber_re').innerHTML=""+Errors[3]+""; return false; } } else { document.getElementById('nunber_re').innerHTML=""+Errors[2]+""; return false; } break; default: document.getElementById('nunber_re').innerHTML=""+Errors[1]+""; return false; } } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics