fix: 增强身份证号码校验逻辑
添加完整的身份证校验规则,包括: 1. 基础格式校验 2. 地区码校验 3. 出生日期有效性检查 4. 校验码计算验证
Showing
1 changed file
with
55 additions
and
3 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2024-01-15 16:35:10 | 2 | * @Date: 2024-01-15 16:35:10 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2026-01-08 20:35:57 | 4 | + * @LastEditTime: 2026-01-20 16:04:01 |
| 5 | * @FilePath: /xyxBooking-weapp/src/pages/addVisitor/index.vue | 5 | * @FilePath: /xyxBooking-weapp/src/pages/addVisitor/index.vue |
| 6 | * @Description: 添加参观者 | 6 | * @Description: 添加参观者 |
| 7 | --> | 7 | --> |
| ... | @@ -88,8 +88,60 @@ const on_id_type_confirm = ({ selectedValue }) => { | ... | @@ -88,8 +88,60 @@ const on_id_type_confirm = ({ selectedValue }) => { |
| 88 | 88 | ||
| 89 | // 身份证校验 | 89 | // 身份证校验 |
| 90 | const checkIDCard = (idcode) => { | 90 | const checkIDCard = (idcode) => { |
| 91 | - // 简单校验 | 91 | + // 1. 基础格式校验 (18位) |
| 92 | - return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idcode); | 92 | + if (!idcode || !/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(idcode)) { |
| 93 | + return false; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + // 2. 地区码校验 | ||
| 97 | + const cityMap = { | ||
| 98 | + 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", | ||
| 99 | + 21: "辽宁", 22: "吉林", 23: "黑龙江", | ||
| 100 | + 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", | ||
| 101 | + 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", | ||
| 102 | + 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", | ||
| 103 | + 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", | ||
| 104 | + 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" | ||
| 105 | + }; | ||
| 106 | + if (!cityMap[idcode.substr(0, 2)]) { | ||
| 107 | + return false; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + // 3. 出生日期校验 | ||
| 111 | + const birthday = idcode.substr(6, 8); | ||
| 112 | + const year = parseInt(birthday.substr(0, 4)); | ||
| 113 | + const month = parseInt(birthday.substr(4, 2)); | ||
| 114 | + const day = parseInt(birthday.substr(6, 2)); | ||
| 115 | + const date = new Date(year, month - 1, day); | ||
| 116 | + | ||
| 117 | + if (date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day) { | ||
| 118 | + return false; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + // 校验日期不能超过当前时间 | ||
| 122 | + if (date > new Date()) { | ||
| 123 | + return false; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + // 4. 校验码计算 | ||
| 127 | + // 加权因子 | ||
| 128 | + const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; | ||
| 129 | + // 校验位对应值 | ||
| 130 | + const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; | ||
| 131 | + | ||
| 132 | + let sum = 0; | ||
| 133 | + const codeArr = idcode.split(""); | ||
| 134 | + // 计算加权和 | ||
| 135 | + for (let i = 0; i < 17; i++) { | ||
| 136 | + sum += codeArr[i] * factor[i]; | ||
| 137 | + } | ||
| 138 | + // 取模 | ||
| 139 | + const mod = sum % 11; | ||
| 140 | + // 获取校验位 | ||
| 141 | + const last = parity[mod]; | ||
| 142 | + | ||
| 143 | + // 对比最后一位 (统一转大写比较) | ||
| 144 | + return last === codeArr[17].toUpperCase(); | ||
| 93 | } | 145 | } |
| 94 | 146 | ||
| 95 | const save = async () => { | 147 | const save = async () => { | ... | ... |
-
Please register or login to post a comment