台湾身份证校验

本文介绍了一个用于验证台湾身份证号码有效性的算法。该算法首先检查身份证号码的长度和格式,然后通过对照地区代码表来验证地区代码,并检查性别字段的有效性。接下来,通过一系列数学运算验证身份证号码的校验位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	/**
	 * 校验台湾身份证格式
	 * @param idCard
	 * @return
	 */
	public static boolean TWIDCardValidate(String idCard){
		if (StringUtils.isNotBlank(idCard) && idCard.length() == 10) {
			idCard = idCard.toUpperCase();
			// 第一位验证:第一位是用户所在的地区
			Hashtable<String,Integer> twMap = GetTWAreaCode();
			Integer tw = twMap.get(idCard.substring(0, 1));
			if (tw == null) {
				return false;
			}
			try {
				// 第二位验证:第二位是性别《1:男,2:女》
				int sex = Integer.parseInt(idCard.substring(1, 2));
				if (sex != 1 && sex != 2) {
					return false;
				}
				// 第三位~第九位:顺序码
				// 十位数
				int ten = tw / 10 % 10;
				// 个位数
				int one = tw % 10;

				int totalNumber = ten; // 区域代码的十位数
				totalNumber += one * 9; // 区域代码的个位数 * 9
				totalNumber += sex * 8; // 性别 * 8

				// 以下都是身份证的第3位 ~ 第9位
				int number = 7;
				for (int i = 2; i < 9; i++) {
					if (number < 1) {
						break;
					}
					totalNumber += Integer.parseInt(idCard.substring(i, i + 1)) * number;
					number --;
				}
				String totalNumberStr = totalNumber+"";
				// 第十位:用 10 减去计算和的各位数
				int lastNumber = 10 - Integer.parseInt((totalNumberStr).substring(totalNumberStr.length() - 1));
				lastNumber = lastNumber == 10 ? 0 : lastNumber;
				if (lastNumber == Integer.parseInt(idCard.substring(9))) {
					return true;
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	private static Hashtable<String, Integer> GetTWAreaCode() {
		Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
		hashtable.put("A", 10);//台北市
		hashtable.put("B", 11);//台中市
		hashtable.put("C", 12);//基隆市
		hashtable.put("D", 13);//台南市
		hashtable.put("E", 14);//高雄市
		hashtable.put("F", 15);//新北市
		hashtable.put("G", 16);//宜兰县
		hashtable.put("H", 17);//桃园市
		hashtable.put("J", 18);//新竹县
		hashtable.put("K", 19);//苗栗县
		hashtable.put("L", 20);//台中县
		hashtable.put("M", 21);//南投县
		hashtable.put("N", 22);//彰化县
		hashtable.put("P", 23);//云林县
		hashtable.put("Q", 24);//嘉义县
		hashtable.put("R", 25);//台南县
		hashtable.put("S", 26);//高雄县
		hashtable.put("T", 27);//屏东县
		hashtable.put("U", 28);//花莲县
		hashtable.put("V", 29);//台东县
		hashtable.put("X", 30);//澎湖县
		hashtable.put("Y", 31);//阳明山
		hashtable.put("W", 32);//金门县
		hashtable.put("Z", 33);//连江县
		hashtable.put("I", 34);//嘉义市
		hashtable.put("O", 35);//新竹市
		return hashtable;
	}
身份证号码共18位,其中前17位为地区和出生年月日信息,最后一位为校验码。以下是基于python实现的身份证校验和信息提取代码: ```python import re def check_idcard(idcard): """ 身份证校验函数 """ # 校验身份证号码格式 if not re.match(r'^\d{17}(\d|X|x)$', idcard): return False # 校验身份证号码的前17位是否合法 province_code = idcard[:2] if province_code not in PROVINCE_CODE: return False birth_year = int(idcard[6:10]) birth_month = int(idcard[10:12]) birth_day = int(idcard[12:14]) if not is_valid_date(birth_year, birth_month, birth_day): return False # 校验身份证号码的最后一位是否正确 weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] check_code = '10X98765432'[sum([int(idcard[i]) * weights[i] for i in range(17)]) % 11] if check_code != idcard[-1].upper(): return False return True def get_idcard_info(idcard): """ 身份证信息提取函数 """ province_code = idcard[:2] province = PROVINCE_DICT.get(province_code, '') birth_year = int(idcard[6:10]) birth_month = int(idcard[10:12]) birth_day = int(idcard[12:14]) sex = '女' if int(idcard[-2]) % 2 == 0 else '男' return { 'province': province, 'birth_year': birth_year, 'birth_month': birth_month, 'birth_day': birth_day, 'sex': sex } def is_valid_date(year, month, day): """ 判断日期是否合法 """ if month < 1 or month > 12: return False if day < 1 or day > 31: return False if month in [4, 6, 9, 11] and day > 30: return False if month == 2: if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: if day > 29: return False else: if day > 28: return False return True # 省份代码和名称映射 PROVINCE_DICT = { '11': '北京市', '12': '天津市', '13': '河北省', '14': '山西省', '15': '内蒙古自治区', '21': '辽宁省', '22': '吉林省', '23': '黑龙江省', '31': '上海市', '32': '江苏省', '33': '浙江省', '34': '安徽省', '35': '福建省', '36': '江西省', '37': '山东省', '41': '河南省', '42': '湖北省', '43': '湖南省', '44': '广东省', '45': '广西壮族自治区', '46': '海南省', '50': '重庆市', '51': '四川省', '52': '贵州省', '53': '云南省', '54': '西藏自治区', '61': '陕西省', '62': '甘肃省', '63': '青海省', '64': '宁夏回族自治区', '65': '新疆维吾尔自治区', '71': '台湾省', '81': '香港特别行政区', '82': '澳门特别行政区' } # 省份代码列表 PROVINCE_CODE = list(PROVINCE_DICT.keys()) ``` 使用示例: ```python idcard = '11010119900307401X' if check_idcard(idcard): idcard_info = get_idcard_info(idcard) print(idcard_info) else: print('身份证号码不合法') ``` 输出结果: ``` {'province': '北京市', 'birth_year': 1990, 'birth_month': 3, 'birth_day': 7, 'sex': '男'} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值