Java验证身份证是否合法

超、凢脫俗 2022-06-07 06:49 280阅读 0赞
  1. import java.text.SimpleDateFormat;
  2. import java.util.Calendar;
  3. import java.util.GregorianCalendar;
  4. import java.util.Hashtable;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.text.ParseException;
  8. /**
  9. * @author open
  10. * 使用方法:调用 IDCardUtil.IDCardValidate(str)验证是否为合法的身份证.返回的是一个String,"YES" 代表合法的身份证 ,其他值代表错误信息
  11. *
  12. */
  13. public class IDCardUtil {
  14. /**
  15. * 验证身份证
  16. * @param IDStr
  17. * @return "YES" 代表合法的身份证 ,其他值代表错误信息
  18. * @throws ParseException
  19. */
  20. public static String IDCardValidate(String IDStr) {
  21. String tipInfo = "YES";// 记录错误信息
  22. String Ai = "";
  23. if(null == IDStr || IDStr.trim().isEmpty())
  24. return "身份证号码长度应该为15位或18位。";
  25. // 判断号码的长度 15位或18位
  26. if (IDStr.length() != 15 && IDStr.length() != 18) {
  27. tipInfo = "身份证号码长度应该为15位或18位。";
  28. return tipInfo;
  29. }
  30. // 18位身份证前17位位数字,如果是15位的身份证则所有号码都为数字
  31. if (IDStr.length() == 18) {
  32. Ai = IDStr.substring(0, 17);
  33. } else if (IDStr.length() == 15) {
  34. Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);
  35. }
  36. if (isNumeric(Ai) == false) {
  37. tipInfo = "身份证15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。";
  38. return tipInfo;
  39. }
  40. // 判断出生年月是否有效
  41. String strYear = Ai.substring(6, 10);// 年份
  42. String strMonth = Ai.substring(10, 12);// 月份
  43. String strDay = Ai.substring(12, 14);// 日期
  44. if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
  45. tipInfo = "身份证出生日期无效。";
  46. return tipInfo;
  47. }
  48. GregorianCalendar gc = new GregorianCalendar();
  49. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
  50. try {
  51. if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
  52. || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
  53. tipInfo = "身份证生日不在有效范围。";
  54. return tipInfo;
  55. }
  56. } catch (NumberFormatException e) {
  57. e.printStackTrace();
  58. } catch (java.text.ParseException e) {
  59. e.printStackTrace();
  60. }
  61. if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
  62. tipInfo = "身份证月份无效";
  63. return tipInfo;
  64. }
  65. if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
  66. tipInfo = "身份证日期无效";
  67. return tipInfo;
  68. }
  69. // 判断地区码是否有效
  70. Hashtable areacode = GetAreaCode();
  71. // 如果身份证前两位的地区码不在Hashtable,则地区码有误
  72. if (areacode.get(Ai.substring(0, 2)) == null) {
  73. tipInfo = "身份证地区编码错误。";
  74. return tipInfo;
  75. }
  76. if (isVarifyCode(Ai, IDStr) == false) {
  77. tipInfo = "身份证校验码无效,不是合法的身份证号码";
  78. return tipInfo;
  79. }
  80. return tipInfo;
  81. }
  82. /*
  83. * 判断第18位校验码是否正确 第18位校验码的计算方式: 1. 对前17位数字本体码加权求和 公式为:S = Sum(Ai * Wi), i =
  84. * 0, ... , 16 其中Ai表示第i个位置上的身份证号码数字值,Wi表示第i位置上的加权因子,其各位对应的值依次为: 7 9 10 5 8 4
  85. * 2 1 6 3 7 9 10 5 8 4 2 2. 用11对计算结果取模 Y = mod(S, 11) 3. 根据模的值得到对应的校验码
  86. * 对应关系为: Y值: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2
  87. */
  88. private static boolean isVarifyCode(String Ai, String IDStr) {
  89. String[] VarifyCode = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
  90. String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
  91. int sum = 0;
  92. for (int i = 0; i < 17; i++) {
  93. sum = sum + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]);
  94. }
  95. int modValue = sum % 11;
  96. String strVerifyCode = VarifyCode[modValue];
  97. Ai = Ai + strVerifyCode;
  98. if (IDStr.length() == 18) {
  99. if (Ai.equals(IDStr) == false) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * 将所有地址编码保存在一个Hashtable中
  107. *
  108. * @return Hashtable 对象
  109. */
  110. private static Hashtable GetAreaCode() {
  111. Hashtable hashtable = new Hashtable();
  112. hashtable.put("11", "北京");
  113. hashtable.put("12", "天津");
  114. hashtable.put("13", "河北");
  115. hashtable.put("14", "山西");
  116. hashtable.put("15", "内蒙古");
  117. hashtable.put("21", "辽宁");
  118. hashtable.put("22", "吉林");
  119. hashtable.put("23", "黑龙江");
  120. hashtable.put("31", "上海");
  121. hashtable.put("32", "江苏");
  122. hashtable.put("33", "浙江");
  123. hashtable.put("34", "安徽");
  124. hashtable.put("35", "福建");
  125. hashtable.put("36", "江西");
  126. hashtable.put("37", "山东");
  127. hashtable.put("41", "河南");
  128. hashtable.put("42", "湖北");
  129. hashtable.put("43", "湖南");
  130. hashtable.put("44", "广东");
  131. hashtable.put("45", "广西");
  132. hashtable.put("46", "海南");
  133. hashtable.put("50", "重庆");
  134. hashtable.put("51", "四川");
  135. hashtable.put("52", "贵州");
  136. hashtable.put("53", "云南");
  137. hashtable.put("54", "西藏");
  138. hashtable.put("61", "陕西");
  139. hashtable.put("62", "甘肃");
  140. hashtable.put("63", "青海");
  141. hashtable.put("64", "宁夏");
  142. hashtable.put("65", "新疆");
  143. hashtable.put("71", "台湾");
  144. hashtable.put("81", "香港");
  145. hashtable.put("82", "澳门");
  146. hashtable.put("91", "国外");
  147. return hashtable;
  148. }
  149. /**
  150. * 判断字符串是否为数字,0-9重复0次或者多次
  151. *
  152. * @param strnum
  153. * @return
  154. */
  155. private static boolean isNumeric(String strnum) {
  156. Pattern pattern = Pattern.compile("[0-9]*");
  157. Matcher isNum = pattern.matcher(strnum);
  158. if (isNum.matches()) {
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. }
  164. /**
  165. * 功能:判断字符串出生日期是否符合正则表达式:包括年月日,闰年、平年和每月31天、30天和闰月的28天或者29天
  166. *
  167. * @param string
  168. * @return
  169. */
  170. public static boolean isDate(String strDate) {
  171. Pattern pattern = Pattern.compile(
  172. "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
  173. Matcher m = pattern.matcher(strDate);
  174. if (m.matches()) {
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. }

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java验证手机号码是否合法

    公司开发新功能需要验证手机号码,遂自己写了个出来,暂只支持中国大陆手机号验证。如有不妥之处,还望大家不吝赐教,感激不尽! / 验证是否是正确合法的

    相关 身份证验证

    18位的身份证号码的最后一位是验证位,在excel中输入身份证号码时能不能使用验证位来判断身份证号码是否输入正确呢,可以使用如下公式(假设身份证号码在A列1行): =MID