验证一个身份证号码, 很简单的需求
原本想搜一个正则,结果发现,错误的太多了,只能本身试着写git
function check (val) { var reg = /[1-9]\d{14}|[1-9]\d{17}|[1-9]\d{16}x/ // 匹配14位或者17位或 16位数字后面加个x的 return reg.test(val) }
解读正则github
[1-9]\d{14}
匹配14位数字
[1-9]\d{17}
匹配17位数字
[1-9]\d{16}x
匹配16位数字后面跟x数组
console.log(check('1111111111111111111111')) // true 这种鬼玩意都能匹配上
在写以前先研究下身份证的结构,这里只研究18位的,由于15位的已通过期了!3d
分析code
xxxxxx yyyy mm ddd nnn v 十八位ci
xxxxxx地区: [1-9]\d{5}
年的前两位: (18|19|20)
年的后两位: \d{2}
月份: ((0[1-9])|(10|11|12))
天数: (([0-2][1-9])|10|20|30|31)
顺序码: \d{3}
验证码: [0-9Xx]
get
正则表达: ^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
it
function check (val) { var reg = /[1-9]\d{14}|[1-9]\d{17}|[1-9]\d{16}x/ // 匹配14位或者17位或 16位数字后面加个x的 return reg.test(val) } console.log(check('610102218801012344')) //年不对 false 610102 2188 01 01 234 4 console.log(check('610102198801012344')) //true 610102 1988 01 01 234 4
身份证的前6位表明地区
对应表 验证中国身份证 前6位对应地区码io
校验码的计算方法console
某男性的身份证号码是340523198001010013。咱们要看看这个身份证是否是合法的身份证。
1.17位分别乘不一样系数 let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
结果相加
let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
的位置的值举个例子
首先咱们得出前17位的乘积和: (3*7+4*9+0*10+5*5+2*8+3*4+1*2+9*1+8*6+0*3+0*7+1*9+0*10+1*5+0*8+0*4+1*2) = 185 而后再求余: 185 % 11 = 9 最后经过对应规则就能够知道余数9对应的数字是3。因此,能够断定这是一个合格的身份证号码。
function check(val) { const city = { '110000': '北京市', '110100': '北京市市辖区', '110101': '北京市东城区', '110102': '北京市西城区'........ } // 对应的地区码 上面有,数据太多这里省略 // 类型转换 let id = parseInt(val, 10) // 正则判断基本类型 if (!/^\d{17}(\d|x)$/i.test(id)) return false // 地区的判断 if (city[id.substr(0, 6)] === undefined) return false // 生日 let birthday = id.substr(6, 4) + '/' + Number(id.substr(10, 2)) + '/' + Number(id.substr(12, 2)) let d = new Date(birthday) let newBirthday = d.getFullYear() + '/' + Number(d.getMonth() + 1) + '/' + Number(d.getDate()) let currentTime = new Date().getTime() let time = d.getTime() if (time >= currentTime || birthday !== newBirthday) return false // 判断校验码 let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] let sum = 0 for (let i = 0; i < 17; i++) { sum += id.substr(i, 1) * arrInt[i] } let residue = arrCh[sum % 11] if (residue !== id.substr(17, 1)) return false return true }