You need to sign in or sign up before continuing.
hookehuyr

优化身份证校验的工具

1 <!-- 1 <!--
2 * @Date: 2022-09-14 14:44:30 2 * @Date: 2022-09-14 14:44:30
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-12 17:40:00 4 + * @LastEditTime: 2024-08-13 15:48:11
5 * @FilePath: /data-table/src/components/IdentityField/index.vue 5 * @FilePath: /data-table/src/components/IdentityField/index.vue
6 * @Description: 身份证输入控件 6 * @Description: 身份证输入控件
7 --> 7 -->
...@@ -63,7 +63,7 @@ import { useRoute } from "vue-router"; ...@@ -63,7 +63,7 @@ import { useRoute } from "vue-router";
63 import $ from "jquery"; 63 import $ from "jquery";
64 import { storeToRefs, mainStore } from "@/utils/generatePackage"; 64 import { storeToRefs, mainStore } from "@/utils/generatePackage";
65 import { showSuccessToast, showFailToast } from "vant"; 65 import { showSuccessToast, showFailToast } from "vant";
66 -import idCard from "idcard"; 66 +// import idCard from "idcard";
67 67
68 const $route = useRoute(); 68 const $route = useRoute();
69 const props = defineProps({ 69 const props = defineProps({
...@@ -99,6 +99,63 @@ watch( ...@@ -99,6 +99,63 @@ watch(
99 } 99 }
100 } 100 }
101 ); 101 );
102 +
103 +var checkProv = function (val) {
104 + var pattern = /^[1-9][0-9]/;
105 + var provs = {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:"澳门"};
106 + if(pattern.test(val)) {
107 + if(provs[val]) {
108 + return true;
109 + }
110 + }
111 + return false;
112 +}
113 +
114 +var checkDate = function (val) {
115 + var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
116 + if(pattern.test(val)) {
117 + var year = val.substring(0, 4);
118 + var month = val.substring(4, 6);
119 + var date = val.substring(6, 8);
120 + var date2 = new Date(year+"-"+month+"-"+date);
121 + if(date2 && date2.getMonth() == (parseInt(month) - 1)) {
122 + return true;
123 + }
124 + }
125 + return false;
126 +}
127 +
128 +var checkCode = function (val) {
129 + var p = /^[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]$/;
130 + var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
131 + var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
132 + var code = val.substring(17);
133 + if(p.test(val)) {
134 + var sum = 0;
135 + for(var i=0;i<17;i++) {
136 + sum += val[i]*factor[i];
137 + }
138 + if(parity[sum % 11] == code.toUpperCase()) {
139 + return true;
140 + }
141 + }
142 + return false;
143 +}
144 +
145 +const idCard = {
146 + verify: function (val) {
147 + if(checkCode(val)) {
148 + var date = val.substring(6,14);
149 + if(checkDate(date)) {
150 + if(checkProv(val.substring(0,2))) {
151 + return true;
152 + }
153 + }
154 + }
155 + return false;
156 + }
157 +}
158 +
102 const readonly = props.item.component_props.readonly; 159 const readonly = props.item.component_props.readonly;
103 const fieldRef = ref(null); 160 const fieldRef = ref(null);
104 const edit_mode = ref(false); 161 const edit_mode = ref(false);
......