身份证号校验(包含长度 特殊字符 区号 出生日期等多个校验)
validator: (rule, value, callback) => {
const arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
const Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
if (value.length != 15 && value.length != 18) {
return callback(new Error('长度格式有误'))
}
let Ai = value.length == 18 ? value.substring(0, 17) : value.slice(0, 6) + "19" + value.slice(6, 16)
if (!/^\d+$/.test(Ai)) { //排除特殊字符X
return callback(new Error('格式有误'))
}
const yyyy = Ai.slice(6, 10), mm = Ai.slice(10, 12) - 1, dd = Ai.slice(12, 14)
const d = new Date(yyyy, mm, dd), now = new Date()
const year = d.getFullYear(), mon = d.getMonth(), day = d.getDate()
if (year != yyyy || mon != mm || day != dd || d > now || year < 1940) {
return callback(new Error('出生日期有误'))
}
let ret = 0
for (let i = 0; i < 17; i++) ret += Ai.charAt(i) * Wi[i]
Ai += arrVerifyCode[ret %= 11]
return value.length == 18 && value != Ai ? callback(new Error('身份证号不合规')) : callback()
}
表格中身份证号脱敏展示
<el-table-column prop="AC_CODE" label="身份证号" width="200" show-overflow-tooltip>
<template slot-scope="scope">
{{scope.row.AC_CODE? scope.row.AC_CODE.replace(/^(.{6})(?:\w+)(.{4})$/,
"\$1******\$2"):""}}
</template>
</el-table-column>