ID number legality verification

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;

/* ******************** Class description ********************
 * class       :  IDCard
 * @author     :  ncc
 * create time : 2017-12-5 11:27:10 am
 * @version    :  1.0  
 * description : valid verification of ID card
 * @see        :                        
 * ************************************************/
public class IDCard {
	/* ********************************************
	 * method name : IDCardValidate description : ID card number verification 1. Number structure
	 * Citizen ID number is a feature combination code, which consists of seventeen-digit body code and one-digit verification code. The arrangement order from left to right is: six-digit address code,
	 * Eight-digit date of birth code, three-digit sequence code and one-digit check code. 2. Address code (the first six digits)
	 * Indicates the administrative division code of the county (city, banner, district) where the permanent residence of the coding object is located, and is implemented in accordance with the provisions of GB/T2260. 3. Date of birth code (seventh to fourteenth digits)
	 * Indicates the year, month, and day of the birth of the coding object, which is implemented in accordance with the provisions of GB/T7408, and there is no separator between the year, month, and day codes. 4. Sequence code (15th to 17th digits)
	 * Indicates the sequence numbers assigned to people born in the same year, month, and day within the area identified by the same address code. The odd number of the sequence code is assigned to males, and the even numbers are assigned to females. 5. Check code (18th digit)
	 * (1) The weighted summation formula of the seventeen-digit ontology code S = Sum(Ai * Wi), i = 0, ... , 16 , first sum the weights of the first 17 digits
	 * Ai: represents the ID number value at the i-th position Wi: represents the weighting factor at the i-th position Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4
	 * 2 (2) Calculate the modulo Y = mod(S, 11) (3) Obtain the corresponding check code Y through the modulo: 0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0
	 * X 9 8 7 6 5 4 3 2
	 * @return : String
	 * @param : @param IDStr ID number
	 * @param : @return Valid: return "certificate number is valid" Invalid: return String information
	 * @param : @throws ParseException modified : ncc , 2017-12-5
	 * @see : *******************************************
	 */
	@SuppressWarnings("unchecked")
	public static String IDCardValidate(String IDStr) throws ParseException {
		// System.out.println("Starting verification...");
		String errorInfo = "";// record error information
		String[] ValCodeArr = { "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" };
		String Ai = "";
		// =============== The length of the number is 15 or 18 digits =================
		if (IDStr.length() != 15 && IDStr.length() != 18) {
			errorInfo = "The ID number should be 15 or 18 digits long.";
			return errorInfo;
		}
		// ================ The numbers are divided into numbers at the end =================
		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) {
			errorInfo = "The 15-digit ID number should be a number; the 18-digit number should be a number except the last one.";
			return errorInfo;
		}
		// =============== Is the date of birth valid ===============
		String strYear = Ai.substring(6, 10);// year
		String strMonth = Ai.substring(10, 12);// month
		String strDay = Ai.substring(12, 14);// month
		if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
			errorInfo = "The birthday of the ID card is invalid.";
			return errorInfo;
		}
		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) {
				errorInfo = "The birthday of the ID card is not within the valid range.";
				return errorInfo;
			}
		} catch (NumberFormatException e) {
			e.printStackTrace ();
		} catch (java.text.ParseException e) {
			e.printStackTrace ();
		}
		if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
			errorInfo = "Invalid ID month";
			return errorInfo;
		}
		if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
			errorInfo = "Invalid ID card date";
			return errorInfo;
		}
		// ================ Is the area code valid?================
		Hashtable h = GetAreaCode();
		if (h.get(Ai.substring(0, 2)) == null) {
			errorInfo = "Incorrect ID card area code.";
			return errorInfo;
		}
		// =============== Determine the value of the last digit =================
		int TotalmulAiWi = 0;
		for (int i = 0; i < 17; i++) {
			TotalmulAiWi = TotalmulAiWi
					+ Integer.parseInt(String.valueOf(Ai.charAt(i)))
					* Integer.parseInt(Wi[i]);
		}
		int modValue = TotalmulAiWi % 11;
		String strVerifyCode = ValCodeArr[modValue];
		Ai = Ai + strVerifyCode;

		if (IDStr.length() == 18) {
			if (Ai.equalsIgnoreCase(IDStr) == false) {
				errorInfo = "Invalid ID card, not a valid ID number!" + Ai;
				return errorInfo;
			}
		} else {
			return "Certificate number is valid";
		}
		// =====================(end)=====================
		return "The ID number is valid!";
	}

	/* ********************************************
	 * method name   : GetAreaCode
	 * description : Function: Set the area code
	 * @return : Hashtable object
	 * @param        : @return
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	@SuppressWarnings("unchecked")
	private static Hashtable GetAreaCode() {
		Hashtable hashtable = new Hashtable();
		hashtable.put("11", "北京");
		hashtable.put("12", "天津");
		hashtable.put("13", "河北");
		hashtable.put("14", "山西");
		hashtable.put("15", "内蒙古");
		hashtable.put("21", "辽宁");
		hashtable.put("22", "吉林");
		hashtable.put("23", "Heilongjiang");
		hashtable.put("31", "上海");
		hashtable.put("32", "江苏");
		hashtable.put("33", "浙江");
		hashtable.put("34", "安徽");
		hashtable.put("35", "福建");
		hashtable.put("36", "江西");
		hashtable.put("37", "山东");
		hashtable.put("41", "河南");
		hashtable.put("42", "湖北");
		hashtable.put("43", "湖南");
		hashtable.put("44", "广东");
		hashtable.put("45", "广西");
		hashtable.put("46", "海南");
		hashtable.put("50", "重庆");
		hashtable.put("51", "四川");
		hashtable.put("52", "贵州");
		hashtable.put("53", "云南");
		hashtable.put("54", "西藏");
		hashtable.put("61", "陕西");
		hashtable.put("62", "甘肃");
		hashtable.put("63", "青海");
		hashtable.put("64", "宁夏");
		hashtable.put("65", "新疆");
		hashtable.put("71", "台湾");
		hashtable.put("81", "香港");
		hashtable.put("82", "澳门");
		hashtable.put("91", "国外");
		return hashtable;
	}

	/* ********************************************
	 * method name   : isNumeric
	 * description : Function: Determine whether the string is a number
	 * @return       : boolean
	 * @param : @param str
	 * @param        : @return
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	private static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher (str);
		if (isNum.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/* ********************************************
	 * method name   : isDate
	 * description : Function: Determine whether the string is in date format
	 * @return       : boolean
	 * @param : @param strDate
	 * @param        : @return
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	public static boolean isDate(String strDate) {
		Pattern pattern = Pattern
				.compile("^((\\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]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
		Matches m = pattern.matcher (strDate);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/* ********************************************
	 * method name   : main
	 * description   :
	 * @return       : void
	 * @param        : @param args
	 * @param        : @throws ParseException
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws ParseException {
		String IDCardNum = "130432198912201314";
		IDCard cc = new IDCard();
		System.out.println(cc.IDCardValidate(IDCardNum));
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485791&siteId=291194637