JavaScript校验身份证,包含省份、长度、出生年月日、校验位的检测、性别、年龄

时间:2022-12-14 14:20:42

一、代码如下

  1 let vcity = {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: '国外 '}
  2 const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  3 const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  4 let birth = ''
  5 // 检查号码是否符合规范,包括长度,类型
  6 function isCardNo (card) {
  7   // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
  8   return /(^\d{15}$)|(^\d{17}(\d|X)$)/.test(card)
  9 }
 10 // 取身份证前两位,校验省份
 11 function checkProvince (card) {
 12   let province = card.substr(0, 2)
 13   return vcity[province] !== undefined
 14   // return true
 15 }
 16 
 17 // 检查生日是否正确
 18 function checkBirthday (card) {
 19   let len = card.length
 20   var arrdata, year, month, day, birthday
 21   // 身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
 22   if (len === 15) {
 23     let refifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/
 24     arrdata = card.match(refifteen)
 25     year = arrdata[2]
 26     month = arrdata[3]
 27     day = arrdata[4]
 28     birthday = new Date('19' + year + '/' + month + '/' + day)
 29     return verifyBirthday('19' + year, month, day, birthday)
 30   }
 31   // 身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
 32   if (len === 18) {
 33     var reeighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/
 34     arrdata = card.match(reeighteen)
 35     year = arrdata[2]
 36     month = arrdata[3]
 37     day = arrdata[4]
 38     birthday = new Date(year + '/' + month + '/' + day)
 39     // console.log(birthday)
 40     return verifyBirthday(year, month, day, birthday)
 41   }
 42   return false
 43 }
 44 // 校验日期
 45 function verifyBirthday (year, month, day, birthday) {
 46   birth = `${year}-${month}-${day}`
 47   var now = new Date()
 48   var nowyear = now.getFullYear()
 49   // 年月日是否合理
 50   if (birthday.getFullYear() === +year && (birthday.getMonth() + 1) === +month && birthday.getDate() === +day) {
 51     // 判断年份的范围(3岁到150岁之间)
 52     var time = nowyear - year
 53     if (time >= 3 && time <= 150) {
 54       return true
 55     }
 56 
 57     return false
 58   }
 59   return false
 60 }
 61 // 校验位的检测
 62 function checkParity (card) {
 63   // 15位转18位
 64   card = changeFivteenToEighteen(card)
 65   var len = card.length
 66   var valnum
 67   var cardTemp = 0
 68   if (len === 18) {
 69     for (let i = 0; i < 17; i++) {
 70       cardTemp += card.substr(i, 1) * arrInt[i]
 71     }
 72     valnum = arrCh[cardTemp % 11]
 73     if (valnum === card.substr(17, 1)) {
 74       return true
 75     }
 76     return false
 77   }
 78   return false
 79 }
 80 function changeFivteenToEighteen (card) {
 81   var cardTemp = 0
 82 
 83   if (card.length === 15) {
 84     card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6)
 85     for (let i = 0; i < 17; i++) {
 86       cardTemp += card.substr(i, 1) * arrInt[i]
 87     }
 88     card += arrCh[cardTemp % 11]
 89     return card
 90   }
 91   return card
 92 }
 93 function getSex (card) {
 94   let sex = 0
 95   let sexNo = 0
 96   if (card.length === 18) {
 97     sexNo = card.substring(16, 17)
 98   } else if (card.length === 15) {
 99     sexNo = card.substring(14, 15)
100   }
101   sex = sexNo % 2
102   if (sex === 0) {
103     sex = 2
104   }
105   return sex
106 }
107 export function getAge (card) {
108   if (card === null) return null
109   let len = card.length
110   var arrdata, year, month, day, age
111 
112   var myDate = new Date()
113   var nowY = myDate.getFullYear()
114   var nowM = myDate.getMonth() + 1
115   var nowD = myDate.getDate() // 获取当前日(1-31)
116   if (len === 15) {
117     let refifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/
118     arrdata = card.match(refifteen)
119     year = arrdata[2]
120     month = arrdata[3]
121     day = arrdata[4]
122 
123     if (nowM > month || (nowM === month && nowD >= day)) { age = nowY - year } else { age = nowY - year - 1 }
124   }
125   if (len === 18) {
126     var reeighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/
127     arrdata = card.match(reeighteen)
128     year = arrdata[2]
129     month = arrdata[3]
130     day = arrdata[4]
131 
132     if (nowM > month || (nowM === month && nowD >= day)) { age = nowY - year } else { age = nowY - year - 1 }
133   }
134   if (age < 0) return null
135   else return age
136 }
137 export function checkIDcard (card) {
138   let errNo = 0
139   if (card === '') {
140     errNo = 1
141   } else if (isCardNo(card) === false) {
142     errNo = 2
143   } else if (checkProvince(card) === false) {
144     errNo = 3
145   } else if (checkBirthday(card) === false) {
146     errNo = 4
147   } else if (checkParity(card) === false) {
148     errNo = 5
149   }
150   return {errno: errNo, birthday: birth, gender: getSex(card), age: getAge(card)}
151 }

在外部主要使用checkIDCard方法即可。