前端JS校驗銀行卡卡號和身份證號碼(附ES6版方法)
/** * 判斷身份證號碼為18位時最後的驗證位是否正確 * @param a_idCard 身份證號碼陣列 * @return */ function isTrueValidateCodeBy18IdCard(a_idCard) { var sum = 0; // 宣告加權求和變數 if (a_idCard[17].toLowerCase() == 'x') { a_idCard[17] = 10; // 將最後位為x的驗證碼替換為10方便後續操作 } for ( var i = 0; i < 17; i++) { sum += Wi[i] * a_idCard[i]; // 加權求和 } valCodePosition = sum % 11; // 得到驗證碼所位置 if (a_idCard[17] == ValideCode[valCodePosition]) { return true; } else { return false; } } /** * 驗證18位數身份證號碼中的生日是否是有效生日 * @param idCard 18位書身份證字串 * @return */ function isValidityBrithBy18IdCard(idCard18){ var year = idCard18.substring(6,10); var month = idCard18.substring(10,12); var day = idCard18.substring(12,14); var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day)); // 這裡用getFullYear()獲取年份,避免千年蟲問題 if(temp_date.getFullYear()!=parseFloat(year) ||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)){ return false; }else{ return true; } } /** * 驗證15位數身份證號碼中的生日是否是有效生日 * @param idCard15 15位書身份證字串 * @return */ function isValidityBrithBy15IdCard(idCard15){ var year = idCard15.substring(6,8); var month = idCard15.substring(8,10); var day = idCard15.substring(10,12); var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day)); // 對於老身份證中的你年齡則不需考慮千年蟲問題而使用getYear()方法 if(temp_date.getYear()!=parseFloat(year) ||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)){ return false; }else{ return true; } } //去掉字串頭尾空格 function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } ES6版校驗方法
const jishuChild1 = []; const jishuChild2 = []; for (let h = 0; h < arrJiShu2.length; h += 1) { jishuChild1.push(parseInt(arrJiShu2[h], 10) % 10); jishuChild2.push(parseInt(arrJiShu2[h], 10) / 10); }
let sumJiShu = 0; let sumOuShu = 0; let sumJiShuChild1 = 0; let sumJiShuChild2 = 0; let sumTotal = 0; for (let m = 0; m < arrJiShu.length; m += 1) { sumJiShu += parseInt(arrJiShu[m], 10); }
for (let n = 0; n < arrOuShu.length; n += 1) { sumOuShu += parseInt(arrOuShu[n], 10); }
for (let p = 0; p < jishuChild1.length; p += 1) { sumJiShuChild1 += parseInt(jishuChild1[p], 10); sumJiShuChild2 += parseInt(jishuChild2[p], 10); } sumTotal = parseInt(sumJiShu, 10) + parseInt(sumOuShu, 10) + parseInt(sumJiShuChild1, 10) + parseInt(sumJiShuChild2, 10); const k = parseInt(sumTotal, 10) % 10 === 0 ? 10 : parseInt(sumTotal, 10) % 10; const luhn = 10 - k; if (lastNum === luhn) { return true; } else { return false; } } 2、身份證號校驗 export function IdCardValidate(idCard) { const idCards = trim(idCard.replace(/ /g, '')); if (idCard.length === 15) { return isValidityBrithBy15IdCard(idCards); } else if (idCards.length === 18) { const idCardS = idCards.split(''); // 得到身份證陣列 if (isValidityBrithBy18IdCard(idCards) && isTrueValidateCodeBy18IdCard(idCardS)) { return true; } else { return false; } } else { return false; }
function isTrueValidateCodeBy18IdCard(aIdCard) { const Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; const ValideCode = ['1', '0', '10', '9', '8', '7', '6', '5', '4', '3', '2']; let sum = 0; const AidCard = aIdCard; if (AidCard[17].toLowerCase() === 'x') { AidCard[17] = '10'; } for (let i = 0; i < 17; i += 1) { sum += Wi[i] * AidCard[i]; } const valCodePosition = sum % 11; if (AidCard[17] === ValideCode[valCodePosition]) { return true; } else { return false; } }
function isValidityBrithBy18IdCard(idCard18) { const year = idCard18.substring(6, 10); const month = idCard18.substring(10, 12); const day = idCard18.substring(12, 14); const tempDate = new Date(year, parseFloat(month) - 1, parseFloat(day)); // 這裡用getFullYear()獲取年份,避免千年蟲問題 if (tempDate.getFullYear() !== parseFloat(year) || tempDate.getMonth() !== parseFloat(month) - 1 || tempDate.getDate() !== parseFloat(day)) { return false; } else { return true; } }
function isValidityBrithBy15IdCard(idCard15) { const year = idCard15.substring(6, 8); const month = idCard15.substring(8, 10); const day = idCard15.substring(10, 12); const tempDate = new Date(year, parseFloat(month) - 1, parseFloat(day)); if (tempDate.getYear() !== parseFloat(year) || tempDate.getMonth() !== parseFloat(month) - 1 || tempDate.getDate() !== parseFloat(day)) { return false; } else { return true; } } function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ''); } }