123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- // 地区码对照表
- const cityCodes = {
- 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 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];
- function validateIDCard(id) {
- // 基础格式校验
- if(!/^\d{17}[\dXx]$/.test(id)) return false;
- // 校验地区码
- if(!cityCodes[id.substr(0,2)]) return false;
- // 校验生日
- const birthDate = id.substr(6,8);
- const year = birthDate.substr(0,4);
- const month = birthDate.substr(4,2);
- const day = birthDate.substr(6,2);
- const date = new Date(year, month-1, day);
- if(date.getFullYear() != year ||
- (date.getMonth()+1) != month ||
- date.getDate() != day) {
- return false;
- }
- // 校验码校验
- let sum = 0;
- for(let i=0; i<17; i++) {
- sum += parseInt(id.charAt(i)) * factor[i];
- }
- const lastChar = id.charAt(17).toUpperCase();
- return parity[sum%11] == lastChar;
- }
- /***************校验手机号***************/
- function validatePhone(phone) {
- return /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/.test(phone);
- }
- /***************校验出生日期与身份证号是否一致***************/
- function getBirthFromID(id) {
- if(id.length === 18) {
- return id.substr(6,4) + '-' + id.substr(10,2) + '-' + id.substr(12,2);
- } else if(id.length === 15) {
- return '19' + id.substr(6,2) + '-' + id.substr(8,2) + '-' + id.substr(10,2);
- }
- return null;
- }
- function validateDate(dateStr) {
- const date = new Date(dateStr.replace(/-/g, '/'));
- return !isNaN(date.getTime());
- }
- function checkBirthMatch(idCard, inputDate) {
- const idBirth = getBirthFromID(idCard); console.log("idBirth="+idBirth) ;
- if(!idBirth || !validateDate(idBirth)) return false;
- // 标准化日期格式比较
- /*const formatDate = (d) => new Date(d).toISOString().split('T');
- console.log("idBirth1="+formatDate(idBirth)) ;
- console.log("idBirth2="+formatDate(inputDate)) ;
- return formatDate(idBirth) === formatDate(inputDate);*/
- return idBirth === inputDate;
- }
- /*********************校验车牌************************/
- function validatePlate(plateNumber) {
- const regularPlate = /^[\u4e00-\u9fa5][A-NP-Z][A-NP-Z0-9]{5}$/; //普通车牌
- const newEnergyPlate = /^[\u4e00-\u9fa5][A-NP-Z]D[A-NP-Z0-9]{6}$/; //新能源车牌
- return regularPlate.test(plateNumber) || newEnergyPlate.test(plateNumber);
- }
|