更新身份证校验

This commit is contained in:
huangdeliang 2020-11-06 13:32:44 +08:00
parent 4264bf8108
commit 5f9109b1ef
3 changed files with 70 additions and 13 deletions
ruoyi-ui/src
main.js
utils
views/custom/signContract

@ -26,7 +26,8 @@ import {
digitUppercase,
selectDictLabel,
selectDictLabels,
toThousands
toThousands,
validatorIDCard
} from "@/utils/ruoyi";
import Pagination from "@/components/Pagination";
//自定义表格工具扩展
@ -44,6 +45,7 @@ Vue.prototype.download = download
Vue.prototype.handleTree = handleTree
Vue.prototype.toThousands = toThousands
Vue.prototype.digitUppercase = digitUppercase
Vue.prototype.validatorIDCard = validatorIDCard
Vue.prototype.msgSuccess = function (msg) {
this.$message({showClose: true, message: msg, type: "success"});

@ -197,3 +197,63 @@ export function digitUppercase(n) {
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整');
};
/*
*
* @params {string} idcode
*
* 函数参数必须是字符串因为二代身份证号码是十八位而在javascript中十八位的数值会超出计算范围造成不精确的结果
* 导致最后两位和计算的值不一致从而该函数出现错误详情查看javascript的数值范围
* 为了避免这一误差idcode必须是字符串
*
* 正则思路
* 第一位不可能是0
* 第二位到第六位可以是0-9
* 第七位到第十位是年份所以七八位为19或者20
* 十一位和十二位是月份这两位是01-12之间的数值
* 十三位和十四位是日期是从01-31之间的数值
* 十五十六十七都是数字0-9
* 十八位可能是数字0-9也可能是X
* */
export function validatorIDCard(idcode) {
if (typeof idcode !== 'string') {
return {
code: -1,
msg: "为了避免javascript数值范围误差idcode 必须是字符串"
}
}
const idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
// 判断格式是否正确
const format = idcard_patter.test(idcode);
if (!format) {
return {
code: -1,
msg: "身份证号码格式错误"
}
}
// 加权因子
const weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
// 校验码
const check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
const last = idcode[17];//最后一位
const seventeen = idcode.substring(0, 17);
// ISO 7064:1983.MOD 11-2
// 判断最后一位校验码是否正确
const arr = seventeen.split("");
const len = arr.length;
let num = 0;
for (let i = 0; i < len; i++) {
num += arr[i] * weight_factor[i];
}
// 获取余数
const resisue = num % 11;
const last_no = check_code[resisue];
// 返回验证结果,校验码和格式同时正确才算是合法的身份证号码
const result = last === last_no ? true : false;
return {
code: result ? 1 : -1,
msg: !result ? "身份证号码格式错误" : ""
}
}

@ -65,7 +65,8 @@
<p>第二条 合作内容及费用
<div class="line-rule">经甲乙双方协商确定乙方向甲方购买 <b>{{form.serveTimeStr}}</b> 胜唐体控瘦身指导服务以下简称服务
经甲乙双方协商一致确定乙方向甲方支付服务费用为人民币 <b>{{form.amount}}</b>大写<b>{{form.amountUpper}}</b></div>
经甲乙双方协商一致确定乙方向甲方支付服务费用为人民币 <b>{{form.amount}}</b>大写<b>{{form.amountUpper}}</b>
</div>
</p>
<p>第三条 服务期约定
@ -105,7 +106,7 @@
<script>
import {getFile, signContract} from "@/api/custom/contract";
import {digitUppercase} from "../../../utils/ruoyi";
import {digitUppercase, validatorIDCard} from "../../../utils/ruoyi";
export default {
name: 'sign',
@ -139,21 +140,15 @@
}
const checkcusId = (rule, value, callback) => {
const phoneReg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/
if (!value) {
return callback(new Error('证件号码不能为空'))
}
setTimeout(() => {
// Number.isIntegeres6,
// +
if (!Number.isInteger(+value)) {
callback(new Error('请输入数字值'))
const {code, msg} = validatorIDCard(value);
if (code === 1) {
callback()
} else {
if (phoneReg.test(value)) {
callback()
} else {
callback(new Error('证件号码格式不正确'))
}
return callback(new Error(msg))
}
}, 100)
}