|
|
|
@ -206,26 +206,6 @@ function arrayExistObjByKeys(arr, keys, values) { |
|
|
|
}; |
|
|
|
exports.arrayExistObjByKeys = arrayExistObjByKeys; |
|
|
|
|
|
|
|
//判断身份证是否合法
|
|
|
|
function checkIDCode(IDCode) { |
|
|
|
// 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
|
|
|
|
let ret = false; |
|
|
|
let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; |
|
|
|
if (reg.test(IDCode)) { |
|
|
|
ret = true; |
|
|
|
} |
|
|
|
if (!ret) return ret |
|
|
|
if (IDCode.length == 15) { |
|
|
|
if (IDCode.substring(8, 10).parseInt > 12 || IDCode.substring(8, 10).parseInt == 0) return false |
|
|
|
if (IDCode.substring(10, 12).parseInt > 31 || IDCode.substring(10, 12).parseInt == 0) return false |
|
|
|
} else { |
|
|
|
if (IDCode.substring(10, 12).parseInt > 12 || IDCode.substring(10, 12).parseInt == 0) return false |
|
|
|
if (IDCode.substring(12, 14).parseInt > 31 || IDCode.substring(12, 14).parseInt == 0) return false |
|
|
|
} |
|
|
|
return ret |
|
|
|
}; |
|
|
|
exports.checkIDCode = checkIDCode; |
|
|
|
|
|
|
|
//根据身份证号判断出生日期(yyyy-MM-DD),当前年龄与性别 粗犷式,不精确判断闰年平年
|
|
|
|
function parseID(id) { |
|
|
|
let ret = { |
|
|
|
@ -483,6 +463,64 @@ function isValidMobileNumber(tel) { |
|
|
|
exports.isValidMobileNumber = isValidMobileNumber; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 简化的身份证验证(仅验证格式和校验码) |
|
|
|
*/ |
|
|
|
function checkIDCode(idCard) { |
|
|
|
idCard = (idCard || '').trim().toUpperCase(); |
|
|
|
|
|
|
|
if (!idCard) return false; |
|
|
|
|
|
|
|
// 格式验证
|
|
|
|
if (!/^\d{17}[\dX]$/.test(idCard) && !/^\d{15}$/.test(idCard)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 15位身份证验证出生日期
|
|
|
|
if (idCard.length === 15) { |
|
|
|
const birthDate = '19' + idCard.substring(6, 12); |
|
|
|
const year = birthDate.substring(0, 4); |
|
|
|
const month = birthDate.substring(4, 6); |
|
|
|
const day = birthDate.substring(6, 8); |
|
|
|
|
|
|
|
const date = new Date(year, month - 1, day); |
|
|
|
return date.getFullYear() === parseInt(year) && |
|
|
|
date.getMonth() === month - 1 && |
|
|
|
date.getDate() === parseInt(day); |
|
|
|
} |
|
|
|
|
|
|
|
// 18位身份证验证校验码
|
|
|
|
if (idCard.length === 18) { |
|
|
|
// 验证出生日期
|
|
|
|
const birthDate = idCard.substring(6, 14); |
|
|
|
const year = birthDate.substring(0, 4); |
|
|
|
const month = birthDate.substring(4, 6); |
|
|
|
const day = birthDate.substring(6, 8); |
|
|
|
|
|
|
|
const date = new Date(year, month - 1, day); |
|
|
|
if (date.getFullYear() !== parseInt(year) || |
|
|
|
date.getMonth() !== month - 1 || |
|
|
|
date.getDate() !== parseInt(day)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 计算校验码
|
|
|
|
const weightingFactors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; |
|
|
|
const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; |
|
|
|
|
|
|
|
let sum = 0; |
|
|
|
for (let i = 0; i < 17; i++) { |
|
|
|
sum += parseInt(idCard.charAt(i)) * weightingFactors[i]; |
|
|
|
} |
|
|
|
|
|
|
|
const mod = sum % 11; |
|
|
|
return checkCodes[mod] === idCard.charAt(17); |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
exports.checkIDCode = checkIDCode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|