生活随笔
收集整理的這篇文章主要介紹了
表单校验 身份证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
表單校驗
//表單中的某一項,校驗身份證<Form.Itemname="idNumber"label="身份證號碼"rules={[{required: true,},({ getFieldValue }) => ({validator(_, value) {if (isChineseIDCardNumber(value)) { //調(diào)用isChineseIDCardNumberreturn Promise.resolve();}return Promise.reject(new Error('您輸入的身份證有誤'),);},}),]}><Inputplaceholder="請輸入身份證號碼"style={{ width: '90%' }}/></Form.Item>
const testRegExp
= /^[1-9]([0-9]{14}|[0-9]{16}[0-9Xx])$/;
const areaMap
= [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,
];
const weightMap
= [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const codeMap
= ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];const isValidDate = (year, month, day) => {const date
= new Date(year
, month
- 1, day
);return (date
.getFullYear() === year
&&date
.getMonth() + 1 === month
&&date
.getDate() === day
&&date
.getTime() < new Date().getTime());
};
export default function isChineseIDCardNumber(value) {const len
= value
.length
;if (len
!== 15 && len
!== 18) {return false;}if (!testRegExp
.test(value
)) {return false;}if (areaMap
.indexOf(+value
.substr(0, 2)) === -1) {return false;}if (len
=== 15) {return isValidDate(+`19${value.substr(6, 2)}`,+value
.substr(8, 2),+value
.substr(10, 2),);}if (!isValidDate(+value
.substr(6, 4),+value
.substr(10, 2),+value
.substr(12, 2),)) {return false;}const sum
= value
.split('').slice(0, 17).reduce((s, num, index) => {return (s
+= +num
* weightMap
[index
]);}, 0);return codeMap
[sum
% 11] === value
[17].toUpperCase();
}
總結(jié)
以上是生活随笔為你收集整理的表单校验 身份证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。