react-記錄前端身份證校驗方法
checkIdNo = (value) => {
const 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]$/;
const q = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$/;
if (value && value.length===18 && p.test(value)) {
const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
const parity = [ '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' ];
const code = value.substring(17);
let sum = 0;
for(let i=0; i < 17; i+=1) {
sum += value[i] * factor[i];
}
if(parity[sum % 11] === code.toUpperCase()) {
return true;
}
}
if (value && value.length===15 && q.test(value)) {
return true;
}
return false;
};