根据身份证的算法 验证身份证是否是正确的身份证号
举个栗: 输入正确的身份证号,随便改变一位数字或者字母(最后一位可能为x) 返回false
注 意: 如果输入的身份号最后一位为x 一定要大写不然永远是false
public class IDCardValidate {
public static boolean validate(String no) {
// 对身份证号进行长度等简单判断
if (no == null || no.length() != 18 || !no.matches("\\d{17}[0-9X]")) {
return false;
}
// 1-17位相乘因子数组
int[] factor = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
// 18位随机码数组
char[] random = "10X98765432".toCharArray();
// 计算1-17位与相应因子乘积之和
int total = 0;
for (int i = 0; i < 17; i++) {
total += Character.getNumericValue(no.charAt(i)) * factor[i];
}
// 判断随机码是否相等
return random[total % 11] == no.charAt(17);
}
}