js验证身份证正则表达式

身份证正则表达式

6位地区号:条件为首位非0,后五位数字——对应正则:[1-9][0-9]{5}
4位年:条件为19或20开头,后两位数字——对应正则:(19|20)[0-9]{2}
4位月日
月份为31天——对应正则:(01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)
月份为30天——对应正则:(04|06|09|11)(0[1-9]|[1-2][0-9]|30)
2月为28天或29天——对应正则:02(0[1-9]|[1-2][0-9])
3位编号:三位数字。对应正则:[0-9]{3}
1位新生成编号:数字或x。对应正则:([0-9]|x|X)

js代码:


var id=/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/
var card=document.getElementById('CardId')
var btn=document.getElementsByTagName('button')[0]
function cardtest (){
if (id.test(card.value)===true){
console.log(card.value)
}
else{
console.log('身份证不正确')
}
}


猜你喜欢

转载自blog.csdn.net/qq_43294510/article/details/88431415