-
验证18的身份证号码
public static void main(String[] args) { String str = "34052419800101001X"; print(IDCardNoVerify(str)); }
-
校验身份证号码最后一位
public static boolean IDCardNoVerify(String IDCardNo) { if (!IDCardNoFormatVerify(IDCardNo)) { return false; } int[] coef = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; int sum = 0; for (int i = 0; i < 17; i++) { sum += Integer.parseInt(IDCardNo.substring(i, i + 1)) * coef[i]; } int remainder = sum % 11; Integer lastNum = ((11 - remainder) + 1) % 11; String last = null; if (lastNum == 10) { last = "X";// 10是罗马数字X表示 } else { last = lastNum.toString(); } if (IDCardNo.endsWith(last)) { return true; } else { return false; } }
-
校验身份证号码的格式
public static boolean IDCardNoFormatVerify(String IDCardNo) { String regexp = "^[0-9]{17}[0-9xX]$"; Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(IDCardNo); return matcher.matches(); }
转载于:https://my.oschina.net/u/3046428/blog/1543494