<script type= ' text/javascript ' src= ' card.js ' ></script> <body> Please enter your ID number: <input name= ' card_no ' type= ' text ' id= ' card_no ' value= ' 1234567890 ' <input type= ' button ' name= ' Submit ' value= ' click me ' onclick= ' javascript:checkcard (); '/> </body> Author:yufulong Blog:http://www.111cn.net */ var vcity={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign" }; Checkcard = function () { var card = document.getElementById (' card_no '). Value; is empty if (Card = = = = ") { Alert (' Please enter the ID number, the ID number cannot be empty '); document.getElementById (' Card_no '). Focus; return false; } Checksum length, type if (Iscardno (card) = = False) { Alert (' The ID number you entered is incorrect, please re-enter '); document.getElementById (' Card_no '). Focus; return false; } Check the provinces if (checkprovince (card) = = False) { Alert (' The ID number you entered is incorrect, please re-enter '); document.getElementById (' Card_no '). Focus; return false; } Verify Birthday if (checkbirthday (card) = = False) { Alert (' The ID number you entered is not a valid birthday, please re-enter '); document.getElementById (' Card_no '). focus (); return false; } Detection of Test bit if (checkparity (card) = = False) { Alert (' Your ID check bit is incorrect, please re-enter '); document.getElementById (' Card_no '). focus (); return false; } Alert (' OK '); return true; }; Check if the number is compliant, including length, type Iscardno = function (card) { The ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits, the first 17 digits, and the last is a check digit, possibly a number or a character X var reg =/(^d{15}$) | (^d{17} (d| X) $)/; if (reg.test (card) = = False) { Alert ("Demo"); return false; } return true; }; Take ID card Two, check the province Checkprovince = function (card) { var province = Card.substr (0,2); if (vcity[province] = = undefined) { return false; } return true; }; Check if birthdays are correct Checkbirthday = function (card) { var len = card.length; ID 15, the order for the province (3) city (3-bit) year (2-bit) month (2-bit) (2-bit) (3-digit) check digit if (len = = ' 15 ') { var re_fifteen =/^ (D{6}) (D{2}) (D{2}) (d{2}) d{3; var arr_data = Card.match (Re_fifteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date (' +year+ '/' +month+ '/' +day); Return Verifybirthday (' +year,month,day,birthday '); } ID 18, the order for the province (3-bit) City (3-bit) year (4-bit) month (2-bit) (2-bit) check bit (4 bit), the check digit end may be X if (len = = ' 18 ') { var re_eighteen =/^ (D{6}) (D{4}) (D{2}) (D{2}) (D{3}) ([0-9]| X) $/; var arr_data = Card.match (Re_eighteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date (year+ '/' +month+ '/' +day); Return Verifybirthday (Year,month,day,birthday); } return false; }; Check Date Verifybirthday = function (year,month,day,birthday) { var now = new Date (); var now_year = Now.getfullyear (); Is it reasonable to date if (birthday.getfullyear () = = Year && (birthday.getmonth () + 1) = = Month && birthday.getdate () = = day) { Determine the range of year (between 3 and 100 years old) var time = now_year-year; if (Time >= 3 && time <= 100) { return true; } return false; } return false; }; Check bit detection checkparity = function (card) { 15-bit ext. 18-bit Card = Changefivteentoeighteen (card); var len = card.length; if (len = = ' 18 ') { var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var cardtemp = 0, I, valnum; for (i = 0; i < i + +) { Cardtemp + + CARD.SUBSTR (i, 1) * arrint[i]; } Valnum = arrch[cardtemp% 11]; if (Valnum = = Card.substr (17, 1)) { return true; } return false; } return false; }; 15-digit ext. 18 ID Number Changefivteentoeighteen = function (card) { if (card.length = ' 15 ') { var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var cardtemp = 0, I; Card = CARD.SUBSTR (0, 6) + ' n ' + card.substr (6, card.length-6); for (i = 0; i < i + +) { Cardtemp + + CARD.SUBSTR (i, 1) * arrint[i]; } Card + + arrch[cardtemp% 11]; return card; } return card; }; |