ID verification
以下为完整Html代码
<!--more-->
<!DOCTYPE html>
<html>
<head lang="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title self-panel-title"> javascriptID验证 </h1>
</div>
<div class="panel-body">
<address class="text-info">
<a href="https://github.com/oxcow/id-number-validator" target="_blank">
<em>github</em>
</a>
</address>
</div>
</div>
<script type="text/javascript">
function getIdCardInfo(cardNo) {
var info = {
isTrue : false,
year : null,
month : null,
day : null,
isMale : false,
isFemale : false
};
if (!cardNo && 15 != cardNo.length && 18 != cardNo.length) {
info.isTrue = false;
return info;
}
if (15 == cardNo.length) {
var year = cardNo.substring(6, 8);
var month = cardNo.substring(8, 10);
var day = cardNo.substring(10, 12);
var p = cardNo.substring(14, 15); //性别位
var birthday = new Date(year, parseFloat(month) - 1,
parseFloat(day));
// 对于老ID中的年龄则不需考虑千年虫问题而使用getYear()方法
if (birthday.getYear() != parseFloat(year)
|| birthday.getMonth() != parseFloat(month) - 1
|| birthday.getDate() != parseFloat(day)) {
info.isTrue = false;
} else {
info.isTrue = true;
info.year = birthday.getFullYear();
info.month = birthday.getMonth() + 1;
info.day = birthday.getDate();
if (p % 2 == 0) {
info.isFemale = true;
info.isMale = false;
} else {
info.isFemale = false;
info.isMale = true
}
}
return info;
}
if (18 == cardNo.length) {
var year = cardNo.substring(6, 10);
var month = cardNo.substring(10, 12);
var day = cardNo.substring(12, 14);
var p = cardNo.substring(14, 17)
var birthday = new Date(year, parseFloat(month) - 1,
parseFloat(day));
// 这里用getFullYear()获取年份,避免千年虫问题
if (birthday.getFullYear() != parseFloat(year)
|| birthday.getMonth() != parseFloat(month) - 1
|| birthday.getDate() != parseFloat(day)) {
info.isTrue = false;
return info;
}
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// 加权因子
var Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];// ID验证位值.10代表X
// 验证校验位
var sum = 0; // 声明加权求和变量
var _cardNo = cardNo.split("");
if (_cardNo[17].toLowerCase() == 'x') {
_cardNo[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
}
for ( var i = 0; i < 17; i++) {
sum += Wi[i] * _cardNo[i];// 加权求和
}
var i = sum % 11;// 得到验证码所位置
if (_cardNo[17] != Y[i]) {
return info.isTrue = false;
}
info.isTrue = true;
info.year = birthday.getFullYear();
info.month = birthday.getMonth() + 1;
info.day = birthday.getDate();
if (p % 2 == 0) {
info.isFemale = true;
info.isMale = false;
} else {
info.isFemale = false;
info.isMale = true
}
return info;
}
return info;
}
</script>
<script type="text/javascript">
function validateNo(){
var cardNo = document.getElementById('cardNo').value;
var cardInfo = getIdCardInfo(cardNo);
var showInfo = '';
if(cardInfo.isTrue){
showInfo = '<span class="text-success">验证通过!</span>';
if(cardInfo.isMale){
showInfo += '<span class="text-info">男,生于 ' + cardInfo.year + '.' + cardInfo.month +
'.' + cardInfo.day + '</span>';
}
if(cardInfo.isFemale){
showInfo += '<span class="text-info">女,生于 ' + cardInfo.year + '.' + cardInfo.month +
'.' + cardInfo.day + '</span>';
}
}else{
showInfo = '<span class="text-error">号码无效!</span>';
}
document.getElementById('cardInfo').innerHTML = showInfo;
}
</script>
<h3 id="验证示例">验证示例</h3>
<div class="row-fluid">
<div class="input-append">
<input id="cardNo" placeholder="请输入ID号码..." type="text">
<button class="btn" type="button" onclick="validateNo();">验证</button>
</div>
<div id="cardInfo" style="margin-left:20px;display:inline"></div>
</div>
</body></html>