Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ID card number verification by JAVA

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve ID card number verification by JAVA". In daily operation, I believe that many people have doubts about how to achieve ID card number verification by JAVA. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to achieve ID card number verification by JAVA". Next, please follow the editor to study!

Package cn.jxjycn.ckadmin.core.utils;import com.ex.framework.web.ApiResult;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Hashtable;import java.util.regex.Matcher;import java.util.regex.Pattern / * @ program: ckadmin * @ description ID card tool class * @ author: ZhangXu * @ create: 2020-09-14 15:12 * ID card number format: 18 digits: 610821-20061222-612Mux * consists of 18 digits: the first 6 digits are address codes, the 7th to 14th digits are the date of birth codes, the 15th to 17th digits are sequence codes, and the 18th digits are check codes. The check code is 0-10 with a total of 11 digits. When the check code is "10", it is indicated by "X" in order to ensure that the citizen ID card number is 18 digits. Although the check code "X" cannot be replaced, if you need to express it all in numbers, you only need to convert the 18-digit citizenship number into a 15-digit identity card number, removing the 7th to 8th digits and the last three digits. * Today's ID card numbers are divided into 15 and 18 digits. China implemented the resident identity card system in 1985, when the ID card number issued at that time was 15 digits, and the ID card issued in 1999 became 18 digits due to the expansion of the year (from two digits to four digits) and the addition of a verification code at the end. * (1) the first 1 and 2 digits indicate the code of the province; * (2) digits 3 and 4 indicate the code of the city; * (3) digits 5 and 6 indicate the code of the district and county; * (4) digits 7 to 14 indicate the year, month and day of birth; * (5) digits 15 and 16 indicate the code of the local police station * (6) the 17th digit indicates gender: an odd number indicates male and an even number indicates female * (7) the 18th digit is a check code: generate * / public class IdCodeUtils {private static Pattern isNumericP = Pattern.compile ("[0-9] *") according to a certain algorithm. Private static Pattern p = Pattern.compile ("(\ d {2} ([02468] [048]) | ([13579] [26])) [\\ -\ /\ s]? (0? [13578]) | (1 [02])) [\ /\ s]? ((0? [1-9]) | (1-2] [0-9]) | (3 [01])) | (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]) | ((0? [13578]) | (11) [\ -\ /\\ s]? (0? [1-9]) | 0-9]) | (30)) | (032 [\\ -\\ /\\ s]? (0? [1-9]) | (1 [0-9]) | (2 [0-8])? $") Public static ApiResult IDCardValidate (String IDStr) throws ParseException {String Ai = ""; / / determine the length of the number 15 or 18 digits if (IDStr.length ()! = 15 & & IDStr.length ()! = 18) {return ApiResult.error ("the ID card number should be 15 or 18 digits.") ;} / / the first 17 digits of an 18-bit ID card. If it is a 15-digit ID card, all numbers are digits if (IDStr.length () = = 18) {Ai = IDStr.substring (0,17);} else if (IDStr.length () = = 15) {Ai = IDStr.substring (0,6) + "19" + IDStr.substring (6,15) } if (isNumeric (Ai) = = false) {return ApiResult.error ("ID card 15-digit numbers should be digits; 18-digit numbers should be digits except the last one.") ;} / / determine whether the date of birth is valid String strYear = Ai.substring (6,10); / / year String strMonth = Ai.substring (10,12); / / month String strDay = Ai.substring (12,14) / date if (isDate (strYear + "-" + strMonth + "-" + strDay) = = false) {return ApiResult.error ("invalid birth date of ID card");} GregorianCalendar gc = new GregorianCalendar (); SimpleDateFormat s = new SimpleDateFormat ("yyyy-MM-dd") Try {if ((gc.get (Calendar.YEAR)-Integer.parseInt (strYear)) > 150 | (gc.getTime () .getTime ()-s.parse (strYear + "-" + strMonth + "-" + strDay) .getTime ())

< 0) { return ApiResult.error("身份证生日不在有效范围。"); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (java.text.ParseException e) { e.printStackTrace(); } if (Integer.parseInt(strMonth) >

| 12 | | Integer.parseInt (strMonth) = = 0) {return ApiResult.error ("ID card month is invalid");} if (Integer.parseInt (strDay) > 31 | | Integer.parseInt (strDay) = = 0) {return ApiResult.error ("invalid ID card date");} / determine whether the area code is valid Hashtable areacode = GetAreaCode () / / if the area code of the first two digits of the ID card is not in Hashtable, the area code is incorrect if (areacode.get (Ai.substring (0,2)) = = null) {return ApiResult.error ("ID card area code error.") ;} if (isVarifyCode (Ai,IDStr) = = false) {return ApiResult.error ("ID card check code is invalid, not a legal ID card number");} return ApiResult.success ();} / * * determine whether the 18th bit check code is correct * the 18th bit check code is calculated: 1. For the first 17 digits, the weighted summation formula is: s = Sum (Ai * Wi), I = 0,..., 16, where Ai represents the digital value of the ID card number in position I, Wi represents the weighting factor in position I, and their corresponding values are: 7 9 10 5 8 4 2 16 3 7 9 10 5 8 4 22 2. Take the module Y = mod (S, 11) 3 with 11 pairs of calculation results. According to the value of the module, the corresponding check code is as follows: y value: 0 1 2 3 4 5 6 7 8 9 9 10 check code: 10 X 9 8 7 6 5 4 3 2 * / private static boolean isVarifyCode (String Ai,String IDStr) {String [] VarifyCode = {"1", "0", "X", "9", "8", "7", "6" "5", "4", "3", "2"} String [] Wi = {"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 ) {sum = sum + Integer.parseInt (String.valueOf (Ai.charAt (I)) * Integer.parseInt (Wii]);} int modValue = sum% 11; String strVerifyCode = VarifyCode [modValue]; Ai = Ai + strVerifyCode; if (IDStr.length () = = 18) {if (Ai.equals (IDStr) = = false) {return false }} return true;} / * Save all address codes in one Hashtable * @ return Hashtable object * / private static Hashtable GetAreaCode () {Hashtable hashtable = new Hashtable (); hashtable.put ("11", "Beijing"); hashtable.put ("12", "Tianjin") Hashtable.put ("13", "Hebei"); hashtable.put ("14", "Shanxi"); hashtable.put ("15", "Inner Mongolia"); hashtable.put ("21", "Liaoning"); hashtable.put ("22", "Jilin"); hashtable.put ("23", "Heilongjiang") Hashtable.put ("31", "Shanghai"); hashtable.put ("32", "Jiangsu"); hashtable.put ("33", "Zhejiang"); hashtable.put ("34", "Anhui"); hashtable.put ("35", "Fujian"); hashtable.put ("36", "Jiangxi") Hashtable.put ("37", "Shandong"); hashtable.put ("41", "Henan"); hashtable.put ("42", "Hubei"); hashtable.put ("43", "Hunan"); hashtable.put ("44", "Guangdong"); hashtable.put ("45", "Guangxi") Hashtable.put ("46", "Hainan"); hashtable.put ("50", "Chongqing"); hashtable.put ("51", "Sichuan"); hashtable.put ("52", "Guizhou"); hashtable.put ("53", "Yunnan"); hashtable.put ("54", "Xizang") Hashtable.put ("61", "Shaanxi"); hashtable.put ("62", "Gansu"); hashtable.put ("63", "Qinghai"); hashtable.put ("64", "Ningxia"); hashtable.put ("65", "Xinjiang"); hashtable.put ("71", "Taiwan") Hashtable.put ("81", "Hong Kong"); hashtable.put ("82", "Macau"); hashtable.put ("91", "overseas"); return hashtable } / * determine whether the string is a number, 0-9 repeat 0 or more times * @ param strnum * @ return * / private static boolean isNumeric (String strnum) {return isNumber (strnum) Function: determines whether the date of birth of a string conforms to the regular expression: including year, month, day, leap year, normal year and 31 days per month, 30 days and 28 days of leap month or 29 days * * @ param strDate * @ return * / public static boolean isDate (String strDate) {return pattern (strDate) } public static boolean isNumber (String str) {Matcher m=isNumericP.matcher (str); return m.matches ();} public static boolean pattern (String str) {Matcher m=p.matcher (str); return m.matches ();}} at this point, the study on "how to implement ID card number verification by JAVA" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report