欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

校验18位身份证号码的正确性

发布时间:2023/12/20 编程问答 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 校验18位身份证号码的正确性 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • 校验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();}`

总结

以上是生活随笔为你收集整理的校验18位身份证号码的正确性的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。