一、正则校验
校验逻辑:
身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
/(^\d{15}$)|(^\d{17}([0-9]|X)$)/i
二、校验方法
1、十五位转十八位
代码如下(示例):
// 校验位取值
const parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
// 加权因子
const power_list = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
let fifteenToEighteen = function(str){
let s = 0;
let newId = str.substring(0,6) + "19" + str.substring(6,str.length);
for (let i=0;i < newId.length;i++ ){
s += parseInt(newId.substring(i,i+1),10)*power_list[i];
}
return newId+parityBit[s%11];
};
2、校验1位校验码(18位)
代码如下(示例):
let checkCodeValidation = function(str){
let s = 0
for (let i=0;i < str.length-1;i++ ){
s += parseInt(str[i],10)*power_list[i];
}
return parityBit[s % 11] === str[17];
};
3、校验6位地址码(前6位)每2位
let locationValidation = function(str){
const provincesAndCities = ['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', '91'];/**省、直辖市代码*/
return provincesAndCities.indexOf(str) !== -1;
};
4、校验8位生日数字(7到14位)
let birthdayValidation = function(str){
let year = str.substring(0,4);
let month = str.substring(4,6);
let day = str.substring(6,8);
let birthday = year+"/"+month+"/"+day;
let date = new Date(birthday);
/**大于等于当前日期 或 小于1900年1月1日*/
return !(date >= new Date() || date <= new Date(1900, 0, 1));
};
三、完整代码
if (value) {
let idCardRex = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/i;// 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
let idCard = value.toString().toUpperCase();// 将末位的x转换成X
let otherCardRex1 = /^[A-Z-0-9()]{8,14}$/,
otherCardRex2 = /^[0-9a-zA-Z]{1,3}字第[0-9a-zA-Z]{4,8}号$/;
const parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];// 校验位取值
const power_list = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];// 加权因子
let fifteenToEighteen = function(str){
let s = 0;
let newId = str.substring(0,6) + "19" + str.substring(6,str.length);
for (let i=0;i < newId.length;i++ ){
s += parseInt(newId.substring(i,i+1),10)*power_list[i];
}
return newId+parityBit[s%11];
};
/**验证1位校验码(18位)*/
let checkCodeValidation = function(str){
let s = 0
for (let i=0;i < str.length-1;i++ ){
s += parseInt(str[i],10)*power_list[i];
}
return parityBit[s % 11] === str[17];
};
/**验证6位地址码(前6位)? 2位*/
let locationValidation = function(str){
const provincesAndCities = ['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', '91'];/**省、直辖市代码*/
return provincesAndCities.indexOf(str) !== -1;
};
/**验证8位生日数字(7到14位)*/
let birthdayValidation = function(str){
let year = str.substring(0,4);
let month = str.substring(4,6);
let day = str.substring(6,8);
let birthday = year+"/"+month+"/"+day;
//var date = new Date(year,parseInt(month,10)-1,day);// ie8 下 parseInt() 默认基数为8, 08,09 会变成 0
let date = new Date(birthday);
/**大于等于当前日期 或 小于1900年1月1日*/
return !(date >= new Date() || date <= new Date(1900, 0, 1));
// return date.toLocaleDateString().substring(0, 10) === birthday;
};
let idCardTest = function () {
if(!(idCardRex.test(idCard))) {
callback(new Error('证件号码输入有误!'));
}
if(15 === idCard.length){// 15位转18位
idCard = fifteenToEighteen(idCard);
}
if ( !(locationValidation(idCard.substring(0,2))// 验证6位地址码(前6位)? 2位
&& birthdayValidation(idCard.substring(6,14))// 验证8位生日数字(7到14位)
&& checkCodeValidation(idCard)) )// 验证1位校验码(18位)
{
callback(new Error('证件号码输入有误!'));
}
}
} else {
callback(new Error('请输入身份证号码!')); // 当身份证表单项为必填时添加
}