Simple implementation of _JAVASCRIPT technique for ID card verification based on JavaScript

Source: Internet
Author: User

This article describes the JavaScript simple to achieve the verification of identity card key code, shared for everyone to refer to, the specific content as follows

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 (card) {//whether is empty if (cards = = = = = ")" Please enter the ID number, the ID number can not be empty "; 
//Checksum length, type if (Iscardno (card) = = False) {return "The ID number you entered is incorrect, please re-enter"; 
Check the province if (checkprovince (card) = = False) {return "The ID number you entered is incorrect, please re-enter"; 
//Verify Birthday if (checkbirthday (card) = = False) {return "Your ID number entered is not the correct birthday, please re-enter"; 
//Check bit detection if (checkparity (card) = = False) {return "Your ID check bit is incorrect, please re-enter"; 
return "OK"; 


}; Check that the number is in line with the specification include length, type Iscardno = function (card) {//ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits before 17 digits, last one is check digit, possibly digit or character x var reg =/(^ \d{15}$) | (^\d{17} (\d| 
X) $)/; 
if (reg.test (card) = = False) {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 that 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, is the number if (len = = ') {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, in the Order of the province (3-bit) City (3-bit) (4-bit) (2-bit) (2-bit) (4-digit) check digit end may be x if (len = = '% ') {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; 

}; Checksum date Verifybirthday = function (year,month,day,birthday) {var now = new Date (); 
var now_year = Now.getfullyear (); Is it reasonable if (birthday.getfullyear () = = Year && (birthday.getmonth () + 1) = = Month && birthday.getdate () = 
= day) {//judge the range of year (between 3 years and 100 years old) var time = now_year-year; 
if (Time >= 3 && time <=) {return true; 
return false; 
return false; 

}; 
Check bit detection checkparity = function (card) {//15-bit to 18-bit card = Changefivteentoeighteen (card); 
var len = card.length; 
if (len = = ') {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 (1)) {return true; 
return false; 
return false; 

}; 15-bit 18-bit ID number Changefivteentoeighteen = function (card) {if (card.length = = ') {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;
 };

The above is the entire content of this article, I hope to realize the JavaScript ID check to help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.