日期:2014-05-16 浏览次数:20334 次
function removeAllSpace(str){ var localString = ''; for(var index = 0; index<str.length; index++) if(str.charCodeAt(index)!= 32){ localString += str.charAt(index); }; return localString; } 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 validId(obj){ var _id=removeAllSpace(obj); if(_id==""){ alert("身份证号码不能为空!"); return false; } var _valid=false; if(_id.length==15){ _valid=validId15(_id); }else if(_id.length==18){ _valid=validId18(_id); } if(!_valid){ alert("身份证号码有误!请您核对"); document.getElementById("cardID").focus(); return false; } return true; } //18 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]); } } var mod=parseInt(_power)%11; if(parityBit[mod]==_parityBit){ return true; } return false; } //15 function validId15(_id){ _id=_id+""; for(var i=0;i<_id.length;i++){ if(_id.charAt(i)<'0'||_id.charAt(i)>'9'){ return false; break; } } var year=_id.substr(6,2); var month=_id.substr(8,2); var day=_id.substr(10,2); var sexBit=_id.substr(14); if(year<'01'||year >'90')return false; if(month<'01'||month >'12')return false; if(day<'01'||day >'31')return false; return true; }