SoFunction
Updated on 2025-02-28

JS Verify whether the real name and ID number match

The interface used in recent projects requires calling real-name authentication. The price of real-name authentication interface is not a few cents higher than that of SMS. Therefore, the conditions for calling real-name authentication must be strictly controlled, so JS is used to verify the real name and JS is used to verify the ID number.

Go to the main topic

JS verification of real names is used to match unicode characters, while Chinese names are generally 2-4, so the match is repeated {2,4} times

Verify the real name

 var regName =/^[\ue-\ufa]{,}$/;
 if(!(name)){
  alert('The real name is filled incorrectly');
  return false;
 }

js verification ID number, Chinese ID number, first generation ID number is a 15-digit number, second generation ID number is all 18-digit, and the last check digit may be 'X' or 'x', so there are four possibilities: a.15-digit number b.18-digit number c.17-digit number, the eighteenth digit is 'X' d.17-digit number, and the eighteenth digit is 'x'

Verify ID number

 var regIdNo = /(^\d{}$)|(^\d{}$)|(^\d{}(\d|X|x)$)/;
 if(!(idNo)){
  alert('Is ID number filled in incorrectly');
  return false;
 }

PS: 15-bit and 18-bit ID card JS verification example

1. Structure and representation of ID number

1. The structure of the number

According to the provisions on citizenship ID numbers in the National Standard GB11643-1999 of the People's *, the citizenship ID number is a characteristic combination code, consisting of a seventeen-digit ontology code and a one-digit verification code. The arrangement order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit verification code.

2. Address code

The administrative division code of the county (city, banner, district) where the permanent residence of the coding object is located shall be implemented in accordance with the provisions of GB/T2260.

3. Date of birth code

It indicates the year, month and day of the birth of the encoded object. It is executed according to the provisions of GB/T7408. There is no separator between the year, month and day codes.

4. Sequence code

It indicates that within the area identified by the same address code, the sequence number assigned to people born in the same year, month, and day. The odd number of the sequence code is assigned to men and even numbers are assigned to women.

5. Verification code

(1) The weighted sum formula for the ontology code of seventeen digits

S = Sum(Ai * Wi), i = 0, ..., 16, first sum of weights of the first 17 digits
Ai: indicates the number value of the ID card number at the i-th position
Wi: represents the weighting factor at the i-th position
7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2) Calculation mode

Y = mod(S, 11)

(3) Obtain the corresponding verification code through the module

Y: 0 1 2 3 4 5 6 7 8 9 10
Verification code: 1 0 X 9 8 7 6 5 4 3 2
That is to say, if the remainder is obtained as 1, the last check bit p should be the corresponding 0.

15-digit number:

a a b b c c y y m m d d x x s

18-digit number:

a a b b c c y y y y m m d d x x s p

2. Address code

The first 6 digits of the ID card are administrative division codes (referred to as digital codes) descriptions (refer to "GB/T 2260-2007 Administrative Division Codes of the People's *"): The principle and structure analysis of the compilation of this digital code adopts a three-layer six-digit hierarchical code structure, which represents the provinces (autonomous regions, municipalities directly under the central government, special administrative regions), cities (regions, autonomous prefectures, leagues), and counties (autonomous counties, county-level cities, banners, autonomous banners, municipal districts, forest areas, special districts) in my country according to the hierarchy.

(1) Number 1 and 2: The code of the province where you are located represents a province, autonomous region, municipality directly under the Central Government, and special administrative regions.
Provincial and municipal code table: { 11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner *", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 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: "*", 71: "*", 81: "*", 82: "Macao", 91: "Foreign"}

(2) The 3rd and 4th digits: the provincial-level city (county) code, which represents the summary code of the districts and counties under the jurisdiction of the city, region, autonomous prefecture, alliance, and municipality directly under the central government, and the summary code of the county-level administrative divisions directly under the provincial (autonomous region).
——01~20, 51~70 means a city, and municipalities directly under the central government say 01 means a city, and after 02 means a suburb county within the jurisdiction of the municipal government;
——21~50 means regions, autonomous prefectures, and alliances;
——90 represents the summary code of county-level administrative divisions directly under the provincial (autonomous region).

(3) 5th and 6th digits: the prefecture-level county (city) code, indicating a county, autonomous county, county-level city, banner, autonomous banner, municipal district, forest area, and special district.
——01~20 means county-level cities under the jurisdiction of the city, region (autonomous prefectures, leagues) under the jurisdiction of county-level cities, municipal special districts, and county-level administrative divisions directly under the jurisdiction of provinces (autonomous regions). 01 usually means the jurisdiction summary code;
——21~80 means a county, autonomous county, banner, autonomous banner, forest area, and regional special zone;
——81~99 means that the province (autonomous region) has a county-level city under its jurisdiction.

3. Others

15-digit ID number:
The 7th and 8th digits are the year of birth (double digits, represented by the last two digits of the year).
No. 9 and 10 are the month of birth.
The 11th and 12th represent the date of birth.
The 13th and 14th bits are the sequential codes, this is a random number.
The 15th digit represents gender, odd numbers are male, and even numbers are female.
18-digit ID number:
The 7th, 8th, 9th and 10th are the year of birth (four digits).
No. 11 and 12 are the months of birth.
The 13th and 14th represent the date of birth.
The 15th and 16th bits are the sequence codes.
The 17th digit represents gender, the odd number is male and the even number is female.
The last bit is the check bit.

4. Js code example

var idCardNoUtil = {
 /*Province, municipality code table*/
 provinceAndCitys: {:"Beijing",:"Tianjin",:"Hebei",:"Shanxi",:"Inner *",:"Liaoning",:"Jilin",:"Heilongjiang",
 :"Shanghai",:"Jiangsu",:"Zhejiang",:"Anhui",:"Fujian",:"Jiangxi",:"Shandong",:"Henan",:"Hubei",:"Hunan",:"Guangdong",
 :"Guangxi",:"Hainan",:"Chongqing",:"Sichuan",:"Guizhou",:"Yunnan",:"Tibet",:"Shaanxi",:"Gansu",:"Qinghai",:"Ningxia",
 :"*",:"*",:"Hongkong",:"Macao",:"foreign"},
 /*Per-bit weighting factor*/
 powers: ["","","","","","","","","","","","","","","","",""],
 /*The first check code*/
 parityBit: ["","","X","","","","","","","",""],
 /*gender*/
 genders: {male:"male",female:"female"},
 /*Check address code*/
 checkAddressCode: function(addressCode){
  var check = /^[-]\d{}$/.test(addressCode);
  if(!check) return false;
  if([parseInt((,))]){
  return true;
  }else{
  return false;
  }
 },
 /*Check date code*/
 checkBirthDayCode: function(birDayCode){
  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))$/.test(birDayCode);
  if(!check) return false; 
  var yyyy = parseInt((,),);
  var mm = parseInt((,),);
  var dd = parseInt((),);
 var xdata = new Date(yyyy,mm-,dd);
 if(xdata > new Date()){
  return false;//The birthday cannot be greater than the current date }else if ( ( () == yyyy ) && (  () == mm - ) && ( () == dd ) ){
  return true;
 }else{
  return false;
 }
 },
 /*Calculation and verification code*/
 getParityBit: function(idCardNo){
 var id = (,); 
  /*Weighted */
  var power = ;
  for(var i=;i<;i++){
  power += parseInt((i),) * parseInt([i]);
  }    
  /*Model*/ 
  var mod = power % ;
  return [mod];
 },
 /*Verify the verification code*/
 checkParityBit: function(idCardNo){
  var parityBit = ().toUpperCase();
  if((idCardNo) == parityBit){
   return true;
  }else{
   return false;
  }
 },
 /*The ID number of the check digit or bit*/
 checkIdCardNo: function(idCardNo){
 //Basic verification of ID number of bits and bits var check = /^\d{}|(\d{}(\d|x|X))$/.test(idCardNo);
 if(!check) return false;
 //Judge the length as bit or bit if(==){
  return (idCardNo);
 }else if(==){
  return (idCardNo);
 }else{
  return false;
 }
 },
 //The ID number of the check digit checkIdCardNo: function(idCardNo){
  //Basic verification of ID number  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}$/.test(idCardNo); 
  if(!check) return false;
  //Check the address code  var addressCode = (,);
  check = (addressCode);
  if(!check) return false;
  var birDayCode = '' + (,);
  //Calculate the date code  return (birDayCode);
 },
 //The ID number of the check digit checkIdCardNo: function(idCardNo){
  //Calculate the basic format of ID number  var check = /^[-]\d{}[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}(\d|x|X)$/.test(idCardNo);
  if(!check) return false;
  //Check the address code  var addressCode = (,);
  check = (addressCode);
  if(!check) return false;
  //Calculate the date code  var birDayCode = (,);
  check = (birDayCode);
  if(!check) return false;
  //Verify the verification code  return (idCardNo); 
 },
 formateDateCN: function(day){
  var yyyy =(,);
  var mm = (,);
  var dd = ();
  return yyyy + '-' + mm +'-' + dd;
 },
 //Get information getIdCardInfo: function(idCardNo){
  var idCardInfo = {
  gender:"", //gender  birthday:"" // Date of birth (yyyy-mm-dd)  };
 if(==){
  var aday = '' + (,);
  =(aday);
  if(parseInt(())%==){
   =;
  }else{
   =;
  }  
 }else if(==){
  var aday = (,);
  =(aday);
   if(parseInt(())%==){
   =;
  }else{
   =;
  } 
 }
 return idCardInfo;
 },
 /*Position index*/
 getId: function(idCardNo){
 if(==){
  return idCardNo;
 }else if(==){
  return (,) + (,); 
 }else{
 return null;
 }
 },
 /*Position index*/
 getId: function(idCardNo){
 if(==){
  var id = (,) + '' + ();
  var parityBit = (id);
  return id + parityBit;
 }else if(==){
  return idCardNo;
 }else{
 return null;
 }
 }
};
//Identity card number verification("idCardNo", function(value, element) { 
 return (element) || (value); 
}, "Please specify a valid ID number."); 
//Get ID card informationvar idCardInfo = (idCardNo); 
alert( + "|" + ); 

Note: When entering and determining whether the same ID card already exists in the database

(1) If you are entering a 15-bit ID: first find out whether the 15-bit ID exists. If it does not exist, you need to convert the 15-bit ID card into an 18-bit ID card. If it still does not exist, you can enter the system.

(2) If you are entering an 18-bit ID: first find out whether the 18-bit ID exists. If it does not exist, you need to convert the 18-bit ID card into a 15-bit ID card. If it still does not exist, you can enter the system.

If you find the corresponding 15-bit ID card, you need to update the 15-bit to 18-bit.