JavaScript code used to verify the validity of Chinese citizen ID card numbers. Js: Chinese citizen
This document uses JavaScript to verify the ID card numbers of Chinese citizens (15 or 18 digits). The functions are as follows:
- ID card number validity Verification
- Analyze detailed ID card information
- Generate a virtual province ID number.
ID card number verification
1. the number structure the citizenship number is a feature combination code, which consists of a 17-digit ontology code and a verification code. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
2. Address Code (first six digits)
Indicates the Administrative Code of the county (city, flag, district) where the resident account of the encoding object is located, which is executed according to GB/T2260.
3. Date of Birth (seventh to fourteen digits)
Indicates the year, month, and day when the encoding object is born. The code is executed according to GB/T7408, and there is no separator between the year, month, and day codes.
4. sequence code (15th-17 digits)
Indicates the sequence number of the person born on the same year, the same month, and the same day within the region specified by the same address code. The odd number of the sequence code is allocated to men, and the even number is allocated to women.
5. Verification Code (18th digits)
(1) The weighted Sum formula S = Sum (Ai * Wi), I = 0 ,... , 16, first sum the right of the first 17 digits
Ai: indicates the number of ID card numbers at location I.
Wi: indicates the weighting factor at position I. Wi: 7 9 10 5 8 8 4 2 1 6 3 7 9 10 5 4 2
(2) computing model Y = mod (S, 11)
(3) obtain the corresponding verification code Y: 0 1 2 3 4 5 6 7 8 9 10 verification code: 1 0X9 8 7 6 5 4 3 2
Here we will use a national address code GB2260, which can be seen below.
/** Http://blog.jdk5.com/zh/javascript-chinese-personal-id-card-validation/#/function IDValidator () {var param = {error: {longNumber: 'There is a precision problem with long numbers. Please use a string! Long number is not allowed, because the precision of the Number In JavaScript. '}}; var util = {checkArg: function (id) {var argType = (typeof id); switch (argType) {case 'number ': // long number not allowed id = id. toString (); if (id. length> 15) {this. error (param. error. longNumber); return false;} break; case 'string': break; default: return false;} id = id. toUpperCase (); var code = null; If (id. length = 18) {// 18-bit code = {body: id. slice (0, 17), checkBit: id. slice (-1), type: 18 };} else if (id. length = 15) {// 15-bit code = {body: id, type: 15 };}else {return false ;}return code ;}// check the address code, checkAddr: function (addr, GB2260) {var addrInfo = this. getAddrInfo (addr, GB2260); return (addrInfo === false? False: true);} // get the address code, getAddrInfo: function (addr, GB2260) {GB2260 = GB2260 | null; // query GB/T2260, if (GB2260 = null) {return addr;} if (! GB2260.hasOwnProperty (addr) {// if the criteria are incomplete, search for var tmpAddr; tmpAddr = addr. slice (0, 4) + '00'; if (! GB2260.hasOwnProperty (tmpAddr) {tmpAddr = addr. slice (0, 2) + '123'; if (! GB2260.hasOwnProperty (tmpAddr) {return false;} else {return GB2260 [tmpAddr] + 'unknown region';} else {return GB2260 [tmpAddr] + 'unknown region ';}} else {return GB2260 [addr] ;}// birthday code check, checkBirth: function (birth) {var year, month, day; if (birth. length = 8) {year = parseInt (birth. slice (0, 4), 10); month = parseInt (birth. slice (4, 6), 10); day = parseInt (birth. slice (-2), 10);} else if (birth. length = 6) {year = parseInt ('19' + birth. slice (0, 2), 10); month = parseInt (birth. slice (2, 4), 10); day = parseInt (birth. slice (-2), 10);} else {return false;} // whether TODO needs to judge the year/** if (year <1800) {return false ;} * // TODO checks if (month> 12 | month = 0 | day> 31 | day = 0) {return false ;} return true;} // check the sequence code, checkOrder: function (order) {// No need to check return true;} // weighted, weight: Function (t) {return Math. pow (2, t-1) % 11;} // random integer, rand: function (max, min) {min = min | 1; return Math. round (Math. random () * (max-min) + min;} // number complement, str_pad: function (str, len, chr, right) {str = str. toString (); len = len | 2; chr = chr | '0'; right = right | false; if (str. length> = len) {return str;} else {for (var I = 0, j = len-str. length; I <j; I ++) {if (right) {Str = str + chr;} else {str = chr + str;} return str ;}// error, error: function (msg) {var e = new Error (); e. message = 'idvalidator: '+ msg; throw e ;}}; var _ IDValidator = function (GB2260) {if (typeof GB2260! = "Undefined") {this. GB2260 = GB2260;} // create cache this. cache ={};}; _ IDValidator. prototype = {isValid: function (id) {var GB2260 = this. GB2260 | null; var code = util. checkArg (id); if (code = false) {return false;} // query cache if (this. cache. hasOwnProperty (id) & typeof this. cache [id]. valid! = 'Undefined') {return this. cache [id]. valid;} else {if (! This. cache. hasOwnProperty (id) {this. cache [id] ={}}} var addr = code. body. slice (0, 6); var birth = (code. type = 18? Code. body. slice (6, 14): code. body. slice (6, 12); var order = code. body. slice (-3); if (! (Util. checkAddr (addr, GB2260) & util. checkBirth (birth) & util. checkOrder (order) {this. cache [id]. valid = false; return false;} // 15 characters without the verification code. if (code. type = 15) {this. cache [id]. valid = true; return true;}/* check bit Section * // position weighted var posWeight = []; for (var I = 18; I> 1; I --) {var wei = util. weight (I); posWeight [I] = wei;} // accumulate the weighted product var bodySum = 0 for the body part and position; var bodyArr = code. body. spl It (''); for (var j = 0; j <bodyArr. length; j ++) {bodySum + = (parseInt (bodyArr [j], 10) * posWeight [18-j]);} // obtain the verification code var checkBit = 12-(bodySum % 11); if (checkBit = 10) {checkBit = 'X';} else if (checkBit> 10) {checkBit = checkBit % 11;} checkBit = (typeof checkBit = 'number '? CheckBit. toString (): checkBit); // check the verification code if (checkBit! = Code. checkBit) {this. cache [id]. valid = false; return false;} else {this. cache [id]. valid = true; return true ;}// analysis details, getInfo: function (id) {var GB2260 = this. GB2260 | null; // The number must be valid if (this. isValid (id) === false) {return false;} // TODO reuse this part of var code = util. checkArg (id); // query cache // by now, the if (typeof this. cache [id]. info! = 'Undefined') {return this. cache [id]. info;} var addr = code. body. slice (0, 6); var birth = (code. type = 18? Code. body. slice (6, 14): code. body. slice (6, 12); var order = code. body. slice (-3); var info ={}; info. addrCode = addr; if (GB2260! = Null) {info. addr = util. getAddrInfo (addr, GB2260);} info. birth = (code. type = 18? ([Birth. slice (0, 4), birth. slice (4, 6), birth. slice (-2)]). join ('-'): (['19' + birth. slice (0, 2), birth. slice (2, 4), birth. slice (-2)]). join ('-'); info. sex = (order % 2 = 0? 0: 1); info. length = code. type; if (code. type = 18) {info. checkBit = code. checkBit;} // record cache this. cache [id]. info = info; return info;} // create an ID, makeID: function (isFifteen) {var GB2260 = this. GB2260 | null; // address code var addr = null; if (GB2260! = Null) {var loopCnt = 0; while (addr = null) {// prevent endless loops if (loopCnt> 10) {addr = 110101; break ;} var prov = util. str_pad (util. rand (50), 2, '0'); var city = util. str_pad (util. rand (20), 2, '0'); var area = util. str_pad (util. rand (20), 2, '0'); var addrTest = [prov, city, area]. join (''); if (GB2260 [addrTest]) {addr = addrTest; break ;}} else {addr = 110101 ;}// Year of birth var yr = util. str_pad (Util. rand (99, 50), 2, '0'); var mo = util. str_pad (util. rand (12, 1), 2, '0'); var da = util. str_pad (util. rand (28, 1), 2, '0'); if (isFifteen) {return addr + yr + mo + da + util. str_pad (util. rand (999, 1), 3, '1');} yr = '19' + yr; var body = addr + yr + mo + da + util. str_pad (util. rand (999, 1), 3, '1'); // position weighted var posWeight = []; for (var I = 18; I> 1; I --) {var wei = util. weight (I); posWeight [I] = wei;} // accumulate the product var bodySum = 0 for the body part and position weighting; var bodyArr = body. split (''); for (var j = 0; j <bodyArr. length; j ++) {bodySum + = (parseInt (bodyArr [j], 10) * posWeight [18-j]);} // obtain the verification code var checkBit = 12-(bodySum % 11); if (checkBit = 10) {checkBit = 'X';} else if (checkBit> 10) {checkBit = checkBit % 11;} checkBit = (typeof checkBit = 'number '? CheckBit. toString (): checkBit); return (body + checkBit) ;}}; // _ IDValidator GB2260 = GB2260 = null? "": GB2260; return new _ IDValidator (GB2260 );}
The call is as follows:
// Create a common instance var Validator = new IDValidator (); // check whether the number is valid. If the number is valid, true is returned. If the number is invalid, falseValidator is returned. isValid (code); // if the number is valid, the analysis information (Region, date of birth, gender, and check digit) is returned. If the number is invalid, falseValidator is returned. getInfo (code); // impersonate an 18-digit ID card number Validator. makeID (); // impersonate a 15-digit ID card number Validator. makeID (true );
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.