JS身份证验证

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script>

function chkreg(){
	var bool=true;
	var card = $('#number').val();
	var str="";
    //是否为空  
    if(card === '')  
    {  
        bool=false;
		str="为空";
	 }  
    //校验长度,类型  
    else if(isCardNo(card) === false)  
    {  
        bool=false;
		str="长度";
    } 
	 //校验不连续重复 
   else if(checkRepeat(card) === false)  
    {  
        bool=false;
		str="连续重复";
    }  
    //检查省份  
    else if(checkProvince(card) === false)  
    {  
        bool=false;  
		str="省份";
    }  

	if(!bool){
		$('#number').focus();
		$("#errortip").text("身份证号码有误:"+str);
	}else{
		$("#errortip").text("");
	}
	return bool;
}

//身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X 
isCardNo = function(card)  
{  
    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;  
    if(reg.test(card) === false)  
    {  
        return false;  
    }  
    return true;  
};

//校验不连续重复
checkRepeat= function(card)  
{  
	var arr=card.split("");
	var a=arr[0];
	 for (var i = 0; i < arr.length; i++) {
		if(arr[i] != a)  
		{  
			return true;
		}  
	}
	return false;
};

  
//取身份证前两位,校验省份  
checkProvince = function(card)  
{  
	var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",  
            21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",  
            33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",  
            42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",  
            51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",  
            63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"  
           };  
    var province = card.substr(0,2);  
    if(vcity[province] == undefined)  
    {  
        return false;  
    }  
    return true;  
};

</script>

<form name="formreg" action="www.baidu.com" method="post" onsubmit="return chkreg();">
                   身份证:<input name="number" type="text" id="number" />
				   <span style="color:#FF0000" id="errortip"></span>
                      <input type="submit" id="subreg" name="button" value="提交" class="mybtn"/>
</form>

  

posted @ 2015-09-15 18:16  暗夜独游Jevil  阅读(214)  评论(0编辑  收藏  举报