During the development process last week, the author encountered the requirement of id card verification. Many parts of the project need verification, so I made a separate file for my own use and share it with you today.

There are two files

If you want to store enumerations, put them in enums.js.

This code is added to enums.js

export const 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 province".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:"The ningxia".65:"Xinjiang".71:"Taiwan".81:"Hong Kong".82:"Macau".91:"Foreign"
};
Copy the code
Two: Verify whether the ID card in the form complies with the rules. You can put it in a separate file, or you can put it in a uniform validation form file in your project.

VerifyTheLegitimacyOfIdCard. Js, add the following:

import { vcity } from '.. /utils/enums';	// Import the constants enums has just added. Please import them according to your own project structure

/** * Verify id card validity *@param CardId ID number String *@returns  Boolean True valid,false invalid */
export const checkTheIdCard = (cardId) = > {
  // Check whether the id card is valid
  return!!!!! (( isCardNo(cardId) && checkProvince(cardId) && checkBirthday(cardId) && checkParity(cardId) )); };// Check the basic input rules
const isCardNo = (cardId) = > {
  const reg = /(^\d{17}(\d|X|x)$)/;
  return reg.test(cardId);
};

// Check whether the first two are provinces
const checkProvince = (cardId) = > {
  const province = cardId.substr(0.2);
  return Boolean(vcity[province]);
};

// Check whether the birthday is correct
const checkBirthday = (cardId) = > {
  const re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/;
  const arr_data = cardId.match(re_eighteen);
  const year = arr_data[2];
  const month = arr_data[3];
  const day = arr_data[4];
  const birthday = new Date(`${year}/${month}/${day}`);
  return (verifyBirthday(year, month, day, birthday));
};

// Test date
const verifyBirthday = (year, month, day, birthday) = > {
  const now = new Date(a);const now_year = now.getFullYear();
  // Determine whether the year, month and day are reasonable
  if (
    birthday.getFullYear() == year &&
    (birthday.getMonth() + 1) == month &&
    birthday.getDate() == day
  ) {
    // Determine the range of years (from 3 to 100 years old)
    const time = now_year - year;
    return (time >= 3 && time <= 100);
  }
  return false;
};

// Check whether the check bit is correct
const checkParity = (cardId) = > {
  const arrInt = [7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2];
  const arrCh = ['1'.'0'.'X'.'9'.'8'.'7'.'6'.'5'.'4'.'3'.'2'];
  let cardTemp = 0, i, valnum;
  for (i = 0; i < 17; i++) {
    cardTemp += cardId.substr(i, 1) * arrInt[i];
  }
  valnum = arrCh[cardTemp % 11];
  console.log('Id card check bit should be:', valnum);
  return (valnum == cardId.substr(17.1));
}
Copy the code
The final step is to use:

Start by importing the checkTheIdCard method into the component where you need to validate your ID rules.

Then verify the ID number separately when submitting the form:

onSubmit = async (saveAndNew = false) = > {// Check if there are any required items left unfilled, etc. If there are all required items, proceed to the following steps:
    // Verify id number is correct
    const idNumber = {};	// {} represents the data in your form and deconstructs the id number from it.
    // if(! IdNumber | | checkTheIdCard (idNumber)) if the id is not mandatory, please change the judgment condition to this
    if (checkTheIdCard(idNumber)) {		// If true, the rule is met
      // Put the code you submit the form in here
    } else {	// If the value is false, a warning is displayed
      return message.error('The ID card number you entered does not match the rules, please re-enter it'); }};Copy the code

In this way, you can import this method to verify the ID number wherever you need to globally.

PS: This method is the latest id card verification code, which does not consider the case of 15 first-generation ID cards. If a friend has special needs can add the author QQ or wechat to ask for. 1336791007