少林

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
<script>
//校验18位的身份证号码
var powers=new Array("7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2");
var parityBit=new Array("1","0","X","9","8","7","6","5","4","3","2");
var sex="male";

 

//校验函数
function validId18(_id){


_id=_id+"";
var _num=_id.substr(0,17);
var _parityBit=_id.substr(17);
var _power=0;
for(var i=0;i< 17;i++){
//校验每一位的合法性
if(_num.charAt(i)<'0'||_num.charAt(i)>'9'){
return false;
break;
}else{
//加权
_power+=parseInt(_num.charAt(i))*parseInt(powers[i]);
//设置性别
if(i==16&&parseInt(_num.charAt(i))%2==0){
sex="female";
}else{
sex="male";
}
}
}
//取模
var mod=parseInt(_power)%11;
if(parityBit[mod]==_parityBit){
return true;
}
return false;


}

 

 

//调用

var b = validId18('456666');
alert(b);
</script>

 

posted on 2015-07-15 11:58  Stanley_yu  阅读(383)  评论(0编辑  收藏  举报