1 var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古", 2 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 3 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南", 4 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆", 5 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃", 6 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外" 7 }; 8 9 checkCard = function() 10 { 11 var card = document.getElementById(‘card_no‘).value; 12 //是否为空 13 if(card === ‘‘) 14 { 15 alert(‘请输入身份证号,身份证号不能为空‘); 16 document.getElementById(‘card_no‘).focus; 17 return false; 18 } 19 //校验长度,类型 20 if(isCardNo(card) === false) 21 { 22 alert(‘您输入的身份证号码不正确,请重新输入‘); 23 document.getElementById(‘card_no‘).focus; 24 return false; 25 } 26 //检查省份 27 if(checkProvince(card) === false) 28 { 29 alert(‘您输入的身份证号码不正确,请重新输入‘); 30 document.getElementById(‘card_no‘).focus; 31 return false; 32 } 33 //校验生日 34 if(checkBirthday(card) === false) 35 { 36 alert(‘您输入的身份证号码生日不正确,请重新输入‘); 37 document.getElementById(‘card_no‘).focus(); 38 return false; 39 } 40 //检验位的检测 41 if(checkParity(card) === false) 42 { 43 alert(‘您的身份证校验位不正确,请重新输入‘); 44 document.getElementById(‘card_no‘).focus(); 45 return false; 46 } 47 alert(‘OK‘); 48 return true; 49 }; 50 51 52 //检查号码是否符合规范,包括长度,类型 53 isCardNo = function(card) 54 { 55 //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X 56 var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 57 if(reg.test(card) === false) 58 { 59 return false; 60 } 61 62 return true; 63 }; 64 65 //取身份证前两位,校验省份 66 checkProvince = function(card) 67 { 68 var province = card.substr(0,2); 69 if(vcity[province] == undefined) 70 { 71 return false; 72 } 73 return true; 74 }; 75 76 //检查生日是否正确 77 checkBirthday = function(card) 78 { 79 var len = card.length; 80 //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字 81 if(len == ‘15‘) 82 { 83 var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; 84 var arr_data = card.match(re_fifteen); 85 var year = arr_data[2]; 86 var month = arr_data[3]; 87 var day = arr_data[4]; 88 var birthday = new Date(‘19‘+year+‘/‘+month+‘/‘+day); 89 return verifyBirthday(‘19‘+year,month,day,birthday); 90 } 91 //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X 92 if(len == ‘18‘) 93 { 94 var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; 95 var arr_data = card.match(re_eighteen); 96 var year = arr_data[2]; 97 var month = arr_data[3]; 98 var day = arr_data[4]; 99 var birthday = new Date(year+‘/‘+month+‘/‘+day); 100 return verifyBirthday(year,month,day,birthday); 101 } 102 return false; 103 }; 104 105 //校验日期 106 verifyBirthday = function(year,month,day,birthday) 107 { 108 var now = new Date(); 109 var now_year = now.getFullYear(); 110 //年月日是否合理 111 if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) 112 { 113 //判断年份的范围(3岁到100岁之间) 114 var time = now_year - year; 115 if(time >= 3 && time <= 100) 116 { 117 return true; 118 } 119 return false; 120 } 121 return false; 122 }; 123 124 //校验位的检测 125 checkParity = function(card) 126 { 127 //15位转18位 128 card = changeFivteenToEighteen(card); 129 var len = card.length; 130 if(len == ‘18‘) 131 { 132 var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 133 var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘); 134 var cardTemp = 0, i, valnum; 135 for(i = 0; i < 17; i ++) 136 { 137 cardTemp += card.substr(i, 1) * arrInt[i]; 138 } 139 valnum = arrCh[cardTemp % 11]; 140 if (valnum == card.substr(17, 1)) 141 { 142 return true; 143 } 144 return false; 145 } 146 return false; 147 }; 148 149 //15位转18位身份证号 150 changeFivteenToEighteen = function(card) 151 { 152 if(card.length == ‘15‘) 153 { 154 var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 155 var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘); 156 var cardTemp = 0, i; 157 card = card.substr(0, 6) + ‘19‘ + card.substr(6, card.length - 6); 158 for(i = 0; i < 17; i ++) 159 { 160 cardTemp += card.substr(i, 1) * arrInt[i]; 161 } 162 card += arrCh[cardTemp % 11]; 163 return card; 164 } 165 return card; 166 };
html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB2312"> <title>Check card no</title> <script type=‘text/javascript‘ src=‘card.js‘></script> </head> <body> 请输入身份证号码: <input name=‘card_no‘ type=‘text‘ id=‘card_no‘ value=‘‘ /> <input type=‘button‘ name=‘submit‘ value=‘click me‘ onclick=‘javascript:checkCard();‘ /> </body> </html>
本文内容来自互联网
时间: 2024-10-20 09:43:10