怎样验证身份证的合法性
各位大哥大姐,小弟现在找一个js验证身份证的合法性的判断,在网上也好了一些用不起来。希望指点一二,谁有更好的贴代码出来看看咯,
------解决方案--------------------function test(){
var v_card=document.getElementById("tests").value;
iW = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
iSum = 0;
for( i=0;i<17;i++){
iC = v_card.charAt(i) ;
iVal = parseInt(iC);
iSum += iVal * iW[i];
}
iJYM = iSum % 11;
var sJYM = '';
if(iJYM == 0) sJYM = "1";
else if(iJYM == 1 ) sJYM = "0";
else if(iJYM == 2 ) sJYM = "x";
else if(iJYM == 3 ) sJYM = "9";
else if(iJYM == 4 ) sJYM = "8";
else if(iJYM == 5 ) sJYM = "7";
else if(iJYM == 6 ) sJYM = "6";
else if(iJYM == 7 ) sJYM = "5";
else if(iJYM == 8 ) sJYM = "4";
else if(iJYM == 9) sJYM = "3";
else if(iJYM == 10) sJYM = "2";
var cCheck = v_card.charAt(17);
if( cCheck != sJYM ){
document.getElementById("s").innerHTML="false"+cCheck+" || "+sJYM;
}else{
document.getElementById("s").innerHTML="true"+cCheck+" || "+sJYM;
}
}
你先用正则判断下输入的是17或14位以非0开头的数字,之后最后一位可以为数字也可以为x或X
------解决方案--------------------HTML code
<html>
<head>
<title>身份证验证</title>
</head>
<body>
<input type="text" value="" id="sfz"/>
<input type="button" value="Test" onclick="Test();"/>
</body>
<script type="text/javascript">
function Test(){
var str=document.getElementById("sfz").value;
//15位身份证
var isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//18位身份证
var isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
var bool=isIDCard1.test(str)||isIDCard2.test(str);
alert(bool)
}
</script>
</html>
------解决方案--------------------
function f_IDCardCheck(as_SourceString) {
return as_SourceString.match(/^[0-9]{6}[1-2]{1}[0-9]{3}[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{1}[0-9]{3}[xX0-9]{1}$/g);
}
上周的项目刚用到,15和18位验证都包括了,这是严格验证的,乱填通不过滴
------解决方案--------------------
不那么严格可以
15位
18位 尾巴是否为x
/^\d{15}(\d{2}[\dxX])?$/