Home > php教程 > php手册 > JS身份证号码有效性验证

JS身份证号码有效性验证

WBOY
Release: 2016-06-07 11:37:43
Original
1193 people have browsed it

如何验证用户输入的身份证号码是否有效?
网上大部分验证方法都只是对身份证号码的格式做了简单的验证,对于身份证的有效性并未做验证,以下这个函数可以验证身份证号码的有效性,支持末尾是X的身份证号码验证
function IdCardValidate(idCard){
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// 加权因子
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]; // 身份证验证位值.10代表X
var sum = 0;
var isValidityBrith = function(year,month,day){
var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
if(year.length == 2){
var temp_year = temp_date.getYear();
}else if(year.length == 4){
var temp_year = temp_date.getFullYear();
}else{
return false;
}
if(temp_year != parseFloat(year)
|| temp_date.getMonth() != parseFloat(month) - 1
|| temp_date.getDate() != parseFloat(day)){
return false;
}else{
return true;
}
}

idCard = idCard.replace(/ /g, "").replace(/(^\s*)|(\s*$)/g, "");
if(idCard.length == 15){
var year = idCard.substring(6,8);
var month = idCard.substring(8,10);
var day = idCard.substring(10,12);
return isValidityBrith(year,month,day);
}
if(idCard.length != 18) return false;
var a_idCard = idCard.split("");
if (a_idCard[17].toLowerCase() == 'x') a_idCard[17] = 10;
for ( var i = 0; i sum += Wi[i] * a_idCard[i];
}
valCodePosition = sum % 11; // 得到验证码所在位置
if (a_idCard[17] != ValideCode[valCodePosition]) return false;
var year = idCard.substring(6,10);
var month = idCard.substring(10,12);
var day = idCard.substring(12,14);
return isValidityBrith(year,month,day);
}

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template