html部分:
1 <input type="text" class="reg_txt" value="身份证号" titles="请输入您的身份证号!" name="userid" id="username" maxlength="40" onfocus="if(this.value=='身份证号'){this.value='';}" onblur="if(this.value==''){this.value='身份证号';}nunber(this.value);" style="border: 1px solid red;"><br> 2 <span class="c_red" id="span_username">span>
javascript部分:
1 function nunber(allowancePersonValue){ 2 if(allowancePersonValue=="身份证号"){ 3 $("#span_username").show(); 4 $("#span_username").html("身份证号不能为空"); 5 return false; 6 } 7 //校验长度,类型 8 else if(isCardNo(allowancePersonValue) === false) 9 { 10 $("#span_username").show(); 11 $("#span_username").html("您输入的身份证号码不正确,请重新输入"); 12 return false; 13 } 14 //检查省份 15 else if(checkProvince(allowancePersonValue) === false) 16 { 17 $("#span_username").show(); 18 $("#span_username").html("您输入的身份证号码不正确,请重新输入"); 19 return false; 20 } 21 //校验生日 22 else if(checkBirthday(allowancePersonValue) === false) 23 { 24 $("#span_username").show(); 25 $("#span_username").html("您输入的身份证号码生日不正确,请重新输入"); 26 return false; 27 } 28 //检验位的检测 29 else if(checkParity(allowancePersonValue) === false) 30 { 31 $("#span_username").show(); 32 $("#span_username").html("您的身份证校验位不正确,请重新输入"); 33 return false; 34 }else{ 35 $("#span_username").hide(); 36 return true; 37 } 38 39 } 40 41 //身份证省的编码 42 var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古", 43 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 44 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南", 45 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆", 46 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃", 47 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外" 48 }; 49 50 //检查号码是否符合规范,包括长度,类型 51 function isCardNo(card){ 52 //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X 53 var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 54 if(reg.test(card) === false){ 55 //alert("demo"); 56 return false; 57 } 58 return true; 59 } 60 61 //取身份证前两位,校验省份 62 function checkProvince(card){ 63 var province = card.substr(0,2); 64 if(vcity[province] == undefined){ 65 return false; 66 } 67 return true; 68 } 69 70 //检查生日是否正确 71 function checkBirthday(card){ 72 var len = card.length; 73 //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字 74 if(len == '15'){ 75 var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; 76 var arr_data = card.match(re_fifteen); 77 var year = arr_data[2]; 78 var month = arr_data[3]; 79 var day = arr_data[4]; 80 var birthday = new Date('19'+year+'/'+month+'/'+day); 81 return verifyBirthday('19'+year,month,day,birthday); 82 } 83 //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X 84 if(len == '18'){ 85 var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; 86 var arr_data = card.match(re_eighteen); 87 var year = arr_data[2]; 88 var month = arr_data[3]; 89 var day = arr_data[4]; 90 var birthday = new Date(year+'/'+month+'/'+day); 91 return verifyBirthday(year,month,day,birthday); 92 } 93 return false; 94 } 95 96 //校验日期 97 function verifyBirthday(year,month,day,birthday){ 98 var now = new Date(); 99 var now_year = now.getFullYear(); 100 //年月日是否合理 101 if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) 102 { 103 //判断年份的范围(3岁到100岁之间) 104 var time = now_year - year; 105 if(time >= 3 && time <= 100) 106 { 107 return true; 108 } 109 return false; 110 } 111 return false; 112 } 113 114 //校验位的检测 115 function checkParity(card){ 116 //15位转18位 117 card = changeFivteenToEighteen(card); 118 var len = card.length; 119 if(len == '18'){ 120 var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 121 var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 122 var cardTemp = 0, i, valnum; 123 for(i = 0; i < 17; i ++) 124 { 125 cardTemp += card.substr(i, 1) * arrInt[i]; 126 } 127 valnum = arrCh[cardTemp % 11]; 128 if (valnum == card.substr(17, 1)) 129 { 130 return true; 131 } 132 return false; 133 } 134 return false; 135 } 136 137 //15位转18位身份证号 138 function changeFivteenToEighteen(card){ 139 if(card.length == '15') 140 { 141 var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 142 var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 143 var cardTemp = 0, i; 144 card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6); 145 for(i = 0; i < 17; i ++) 146 { 147 cardTemp += card.substr(i, 1) * arrInt[i]; 148 } 149 card += arrCh[cardTemp % 11]; 150 return card; 151 } 152 return card; 153 }
转载自:https://www.jb51.net/article/73613.htm