在程序中我们总免不了对用户输入的数据进行上层校验,将格式不正确的数据直接拦截在调用整个方法之前,这样不仅能让用户很明确地知道自己输入的数据是否正确,错误的原因,还一定程度上提高了方法的效率。小编在最近的工作中,整理了关于身份证号的验证方式:
I 使用最朴实的Java代码实现
我们都知道身份证号的组成受地区、出生年月的影响,所以工具类中自然少不了对这两个要素的判断:
// 地区集合
final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();
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(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 certNo
* @return
*/
public static boolean isIDCard(String certNo) {
if (certNo == null || (certNo.length() != 15 && certNo.length() != 18))
return false;
final char[] cs = certNo.toUpperCase().toCharArray();
// 校验位数
int power = 0;
for (int i = 0; i < cs.length; i++) {
if (i == cs.length - 1 && cs[i] == 'X')
break;// 最后一位可以 是X或x
if (cs[i] < '0' || cs[i] > '9')
return false;
if (i < cs.length - 1) {
power += (cs[i] - '0') * POWER_LIST[i];
}
}
// 校验区位码
if (!zoneNum.containsKey(Integer.valueOf(certNo.substring(0, 2)))) {
return false;
}
// 校验年份
String year = certNo.length() == 15 ? getIdcardCalendar() + certNo.substring(6, 8) : certNo.substring(6, 10);
final int iyear = Integer.parseInt(year);
if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))
return false;// 1900年的PASS,超过今年的PASS
// 校验月份
String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10, 12);
final int imonth = Integer.parseInt(month);
if (imonth < 1 || imonth > 12) {
return false;
}
// 校验天数
String day = certNo.length() == 15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
final int iday = Integer.parseInt(day);
if (iday < 1 || iday > 31)
return false;
// 校验"校验码"
if (certNo.length() == 15)
return true;
return cs[cs.length - 1] == PARITYBIT[power % 11];
}
/**
* 获取身份证号中的年月日
*
* @return
*/
private static int getIdcardCalendar() {
GregorianCalendar curDay = new GregorianCalendar();
int curYear = curDay.get(Calendar.YEAR);
int year2bit = Integer.parseInt(String.valueOf(curYear).substring(2));
return year2bit;
}
但是技术总是发展的,从某天开始,攻城狮们获得了一项福利--可以将上述繁杂的代码使用一个表达式来概括,那就是 正则表达式:
II 正则表达式:
public static boolean verifyIdCard(String idCard){
String strVerify = "(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}(\\d|x|X)$)|(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)";
Pattern p = Pattern.compile(strVerify);
Matcher m = p.matcher(idCard);
return m.find();
}
在开发的工作中,我们应该尽量地提高代码的精炼程度,相比之下,小编还是比较倾向于正则表达式,毕竟两种方式的代码量差距实在太大了。不过上面都是小编的个人见解,希望能帮大家在进行数据校验的过程中解决疑惑,缩短查阅的时间!!