啥也不说了,直接上代码,直接调用即可
1 public class IDCardUtils { 2 /** 3 * 身份证号码正则表达式 4 */ 5 public static boolean isIDCard(String idNum) throws ParseException { 6 //定义判别用户身份证号的正则表达式(要么是15位,要么是18位,最后一位可以为字母) 7 // Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])"); 8 // //通过Pattern获得Matcher 9 // Matcher idNumMatcher = idNumPattern.matcher(idNum); 10 // return idNumMatcher.matches(); 11 String[] ValCodeArr = { "1", "0", "x", "9", "8", "7", "6", "5", "4", 12 "3", "2" }; 13 String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", 14 "9", "10", "5", "8", "4", "2" }; 15 String Ai = ""; 16 // ================ 号码的长度 15位或18位 ================ 17 if (idNum.length() != 15 && idNum.length() != 18) { 18 return false; 19 } 20 // =======================(end)======================== 21 22 // ================ 数字 除最后以为都为数字 ================ 23 if (idNum.length() == 18) { 24 Ai = idNum.substring(0, 17); 25 } else if (idNum.length() == 15) { 26 Ai = idNum.substring(0, 6) + "19" + idNum.substring(6, 15); 27 } 28 if (isNumeric(Ai) == false) { 29 return false; 30 } 31 // =======================(end)======================== 32 33 // ================ 出生年月是否有效 ================ 34 String strYear = Ai.substring(6, 10);// 年份 35 String strMonth = Ai.substring(10, 12);// 月份 36 String strDay = Ai.substring(12, 14);// 月份 37 if (isDataFormat(strYear + "-" + strMonth + "-" + strDay) == false) { 38 return false; 39 } 40 GregorianCalendar gc = new GregorianCalendar(); 41 SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); 42 if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 43 || (gc.getTime().getTime() - s.parse( 44 strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) { 45 return false; 46 } 47 if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) { 48 return false; 49 } 50 if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) { 51 return false; 52 } 53 // =====================(end)===================== 54 55 // ================ 地区码时候有效 ================ 56 Hashtable h = GetAreaCode(); 57 if (h.get(Ai.substring(0, 2)) == null) { 58 return false; 59 } 60 // ============================================== 61 62 // ================ 判断最后一位的值 ================ 63 int TotalmulAiWi = 0; 64 for (int i = 0; i < 17; i++) { 65 TotalmulAiWi = TotalmulAiWi 66 + Integer.parseInt(String.valueOf(Ai.charAt(i))) 67 * Integer.parseInt(Wi[i]); 68 } 69 int modValue = TotalmulAiWi % 11; 70 String strVerifyCode = ValCodeArr[modValue]; 71 Ai = Ai + strVerifyCode; 72 73 if (idNum.length() == 18) { 74 if (Ai.equalsIgnoreCase(idNum) == false) { 75 return false; 76 } 77 } else { 78 return true; 79 } 80 return true; 81 82 } 83 84 /** 85 * 功能:判断字符串是否为数字 86 * @param str 87 * @return 88 */ 89 private static boolean isNumeric(String str) { 90 Pattern pattern = Pattern.compile("[0-9]*"); 91 Matcher isNum = pattern.matcher(str); 92 if (isNum.matches()) { 93 return true; 94 } else { 95 return false; 96 } 97 } 98 99 /**100 * 功能:设置地区编码101 * @return Hashtable 对象102 */103 private static Hashtable GetAreaCode() {104 Hashtable hashtable = new Hashtable();105 hashtable.put("11", "北京");106 hashtable.put("12", "天津");107 hashtable.put("13", "河北");108 hashtable.put("14", "山西");109 hashtable.put("15", "内蒙古");110 hashtable.put("21", "辽宁");111 hashtable.put("22", "吉林");112 hashtable.put("23", "黑龙江");113 hashtable.put("31", "上海");114 hashtable.put("32", "江苏");115 hashtable.put("33", "浙江");116 hashtable.put("34", "安徽");117 hashtable.put("35", "福建");118 hashtable.put("36", "江西");119 hashtable.put("37", "山东");120 hashtable.put("41", "河南");121 hashtable.put("42", "湖北");122 hashtable.put("43", "湖南");123 hashtable.put("44", "广东");124 hashtable.put("45", "广西");125 hashtable.put("46", "海南");126 hashtable.put("50", "重庆");127 hashtable.put("51", "四川");128 hashtable.put("52", "贵州");129 hashtable.put("53", "云南");130 hashtable.put("54", "西藏");131 hashtable.put("61", "陕西");132 hashtable.put("62", "甘肃");133 hashtable.put("63", "青海");134 hashtable.put("64", "宁夏");135 hashtable.put("65", "新疆");136 hashtable.put("71", "台湾");137 hashtable.put("81", "香港");138 hashtable.put("82", "澳门");139 hashtable.put("91", "国外");140 return hashtable;141 }142 }
希望可以帮到大家!