ID number verification (js)

var vcity = {
. 11: "Beijing",
12: "Tianjin",
13: "Hebei",
14: "Journal of Shanxi",
15: "Inner Mongolia",
21: "Liaoning",
22: "Jilin",
23: "SCIENCE "
31:" Shanghai ",
32:" Jiangsu ",
33:" Zhejiang ",
34:" Journal ",
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"
}

CheckIDCard function (Card) {
// is empty
IF (Card === '') {
Alert ( 'Please input ID number, the ID number can not be empty');
return to false;
}
// check the length, type
IF (isCardNo (Card) === false) {
Alert ( 'identity card number you entered is incorrect, please try again');
return false;
}
// check the province
if (checkProvince (card) === false ) {
alert ( 'identity card number you entered is incorrect, please try again');
return false;
}
// check birthday
IF (checkBirthday (Card) === false) {
alert ( 'You do not enter the ID number birthday correct, please re-enter ');
return to false;
}
// check bit detector
IF (checkparity (Card) === to false) {
Alert (' your ID parity bit is incorrect, please re-enter ');
return to false;
}
Alert ( 'the OK');
return to true;
}

// Check whether the number of specifications, including length, type
isCardNo = function (Card) {
// identification number is 15 or 18, when all numbers 15, 18 into a digital front 17, the last one is parity bit, may be numbers or characters X-
var = REG / (^ \ D {15} $) | (^ \ {D}. 17 (\ D | X-) $) /;
IF (reg.test (Card) = to false ==) {
return to false;
}

return true;

}

// get the first two identification checksum provinces
checkProvince = function (Card) {
var card.substr Province = (0, 2);
IF (vcity [Province] == undefined) {
return to false;
}
return to true;
}

// Check whether the correct date of birth
checkBirthday = function (Card) {
var len = card.length;
// the ID card 15, the order of the province (3) City (3) years (2) February (2) 1999 (2) parity bit (3), are all digital
IF (len == '15') {
var re_fifteen = / ^ (\. 6 {D}) (\ D {2}) (\ D {2 }) (\ D {2}) (\ {D}. 3) $ /;
var = arr_data card.match (re_fifteen);
var = arr_data year [2];
var = month The arr_data [. 3];
var Day = arr_data [ . 4];
var = new new Birthday a Date ('19 'year + +' / '+ month The +' / '+ Day);
return verifyBirthday ('19' + year, month The, Day, Birthday);
}
// identity 18 position, the order of the province (3) City (3) years (four) months (2) day (2) parity bit (4), the end of the parity bit may be the X-
IF (len == '18 is') {
var re_eighteen = / ^ (\. 6 {D}) (\. 4 {D}) (\ 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;
}

// calibration date
verifyBirthday = function (year, month The, Day, Birthday) {
var now = new new a Date ();
var now_year now.getFullYear = ();
// date is reasonable
if (birthday.getFullYear () = year && = (birthday.getMonth (). 1 +) && birthday.getDate month the == () == Day) {
// Year judgment (between 100 years to 3 years)
var Time = now_year - year;
IF ( Time> =. 3 && Time <= 100) {
return to true;
}
return to false;
}
return to false;
}

//校验位的检测
checkParity = function(card) {
//15位转18位
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 < 17; 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位转18位身份证号
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) + '19' + card.substr(6, card.length - 6);
for (i = 0; i < 17; i++) {
cardTemp += card.substr(i, 1) * arrInt[i];
}
card += arrCh[cardTemp % 11];
return card;
}
return card;
}

Guess you like

Origin www.cnblogs.com/hjworld/p/12123454.html