javascript based on the simple realization of identity card verification

  • 2020-12-21 17:56:40
  • OfStack

This article introduces the key code of javascript simple verification of id card, and shares it with you for your reference. The specific content is as follows


var vcity={ 11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ", 
21:" liaoning ",22:" Ji Lin ",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:"4 sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet ",61:" shaanxi ",62:" gansu ", 
63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" foreign " 
}; 

checkCard = function(card) 
{ 

// Whether is empty  
if(card === '') 
{ 

return " Please input id number, id number cannot be empty "; 
} 
// Check length, type  
if(isCardNo(card) === false) 
{ 

return " The ID number you entered is not correct, please re-enter it "; 
} 
// Check the province  
if(checkProvince(card) === false) 
{ 
return " The ID number you entered is not correct , Please re-enter "; 
} 
// Check the birthday  
if(checkBirthday(card) === false) 
{ 
return " The id number you entered is incorrect , Please re-enter "; 
} 
// The detection of check bits  
if(checkParity(card) === false) 
{ 
return " Your id check digit is incorrect , Please re-enter "; 
} 

return "ok"; 
}; 


// Check if the number conforms to the specification, including length, type  
isCardNo = function(card) 
{ 
// Id Card No. 15 Bits or 18 position 15 The bits are all numbers, 18 former 17 The digits are numbers, and finally 1 A bit is a check bit and may be a number or character X 
var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 
if(reg.test(card) === false) 
{ 
return false; 
} 

return true; 
}; 

// Take the first two id cards , Check provinces  
checkProvince = function(card) 
{ 
var province = card.substr(0,2); 
if(vcity[province] == undefined) 
{ 
return false; 
} 
return true; 
}; 

// Check if the birthday is correct  
checkBirthday = function(card) 
{ 
var len = card.length; 
// Id card 15 Bit, the order is province ( 3 (a) 3 A) in ( 2 (a) month 2 (a), 2 Bit) Check bit ( 3 Digits), are numbers  
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('19'+year+'/'+month+'/'+day); 
return verifyBirthday('19'+year,month,day,birthday); 
} 
// Id card 18 Bit, the order is province ( 3 (a) 3 A) in ( 4 (a) month 2 (a), 2 Bit) Check bit ( 4 ), the end of the check bit 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 the date  
verifyBirthday = function(year,month,day,birthday) 
{ 
var now = new Date(); 
var now_year = now.getFullYear(); 
// Is the year month day reasonable  
if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) 
{ 
// Range of Year ( 3 To the age of 100 Between the ages of ) 
var time = now_year - year; 
if(time >= 3 && time <= 100) 
{ 
return true; 
} 
return false; 
} 
return false; 
}; 

// Check bit detection  
checkParity = function(card) 
{ 
//15 A turn 18 position  
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 A turn 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) + '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; 
};

Above is the entire content of this article, I hope to help you to implement javascript id check.


Related articles: