知识:js校验身份证号、根据身份证号获取出生日期、年龄、性别
一、15或18位身份证号输入校验:
代码演示:
注意:match用法见此连接(还没写好,等写好再放链接)
// 一、身份证正则表达式:
// 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一为是校验位,可能为数字或者字符X
const ID_CARD_REGX = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/
const ID_CARD_15_REGX = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/ // 15位身份证,年份为2位数,为后两位,例如78、79、80等等
const ID_CARD_18_REGX = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/
// 二、校验年、月、日是否正确
const checkIdDate = (num, re) => {
const arrSplit = num.match(re) // ['110101198007120018', '110101', '1980', '07', '12', '001', 8]
const birth = new Date(`${arrSplit[2]}/${arrSplit[3]}/${arrSplit[4]}`) // '1980/07/12'
const isGoodDay = ((num.length == 15 ? birth.getYear() : birth.getFullYear()) == Number(arrSplit[2])) && // 校验年份
((birth.getMonth() + 1) == Number(arrSplit[3])) && // 校验月份
(birth.getDate() == Number(arrSplit[4])) // 校验日期
return isGoodDay
}
// 三、真实身份证校验:
// return true则表示通过校验,false表示未通过校验
const checkIdCard = str => {
str = str.toUpperCase()
const len = str.length
// 1. 身份证长度校验
if (!ID_CARD_REGX.test(str) || len < 15) return false
// 2. 身份证年月日校验
let re
if (len == 15) {
re = new RegExp(ID_CARD_15_REGX)
} else if (len == 18) {
re = new RegExp(ID_CARD_18_REGX)
}
if (!checkIdDate(str, re)) return false
// 3. 18位身份证ISO校验
// 校验位按照ISO 7064:1983.MOD 11 - 2的规则生成,X可认为是数字10
const arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
const arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
let nTemp = 0, valNum, i;
for (i = 0; i < 17; i++) nTemp += str.substr(i, 1) * arrInt[i]
valNum = arrCh[nTemp % 11]
if (len === 18 && valNum != str.substr(17, 1)) {
return false
}
return true
}
// 四、试验:
checkIdCard('110101800712001')
checkIdCard('11010119800712027X')
二、根据身份证号获取出生年月日:
例如:2023/01/01、2023/10/10
const getBirthdayByIdCard = idCard => {
if (idCard.length === 15) {
return `19${idCard.substr(6, 2)}/${idCard.substr(8, 2)}/${idCard.substr(10, 2)}`
} else if (idCard.length === 18) {
return `${idCard.substr(6, 4)}/${idCard.substr(10, 2)}/${idCard.substr(12, 2)}`
} else {
return ''
}
}
getBirthdayByIdCard('110101800712001') // 15位身份证:1980/07/12
getBirthdayByIdCard('11010119800712027X') // 18位身份证:1980/07/12
三、根据出生日期获取年龄:
例如:出生于2000/01/01到2023/10/10年龄为23岁
注意:日常生活中用到的都是周岁
const getAgeByBirthday = birth => {
let age= 0
const birthYear = birth.substr(0, 4)
const birthMonth = birth.substr(5, 2)
const birthDay = birth.substr(8, 2)
const nowDate = new Date()
const nowYear = nowDate.getFullYear()
const nowMonth = nowDate.getMonth() + 1
const nowDay = nowDate.getDate()
age = nowYear - birthYear
if (nowMonth < Number(birthMonth) || (nowMonth == Number(birthMonth) && nowDay < Number(birthDay))) age--
return age
}
getAgeByBirthday('2000/01/01') // 23
getAgeByBirthday('2000/11/11') // 22
四、根据身份证号获取性别
const genderArr = ['男', '女']
const getGenderByIdCard = idCard => {
if (idCard.length === 15) {
return genderArr[idCard.substr(14, 1) % 2]
} else if (idCard.length === 18) {
return genderArr[idCard.substr(16, 1) % 2]
} else {
return '未知'
}
}
getBirthdayByIdCard('110101800712001') // 15位身份证:1980/07/12
getBirthdayByIdCard('11010119800712027X') // 18位身份证:1980/07/12