The following simple code introduces the method of intercepting ID card number encryption for regular expressions. The specific code is as follows:
Directly upload the code:
var idCard = '420119188404098978X'; //Retain the first four and the last four digits, and use * to encrypt the middle.idCard = (/(\d{4})\d*([0-9a-zA-Z]{4})/,"$1******$2"); //result:4201******978X
PS: The following is a relatively standardized regular expression for ID number verification
Some places where user information is strictly required, such as when registering and logging in, you need to verify whether the user's ID card is legal. Through these verifications, many scuba customers can be greatly filtered out and bring very accurate user information to your system.
Many times we use a set of regular expressions to determine whether the ID card entered by the user is legal. Before using regular expressions to judge, how much do you know about the composition of the ID card number? Let’s talk about how much information is contained in an ID number:
1. The structure of the number
The citizenship number is a feature 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 (first six digits)
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 (7th to 14th digits)
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 (fifth to seventeenth digits)
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 (eighteenth digit)
As the verification code of the last number, it is calculated by the number compilation unit according to a unified formula. If the last number of someone is 0-9, X will not appear, but if the last number is 10, then X must be used instead, because if 10 is used as the last number, then the ID card of this person becomes 19 digits. X is the 10 of the Roman numeral. Replacing 10 with X can ensure that the citizen's ID card meets national standards.
After knowing the meaning of the various parts of the ID number structure, we start to enter the topic:
1. Define an object in a national region
var aCity={11:"Beijing",12:"Tianjin",13:"Hebei",14:"Shanxi",15:"Inner *",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:"*",71:"*",81:"Hongkong",82:"Macao",91:"foreign"}
2. Regular expression judgment
function isCardID(sId){ var iSum=0 ; var info="" ; if(!/^\d{17}(\d|x)$/(sId)) return "The length or format of the ID card you entered"; sId=(/x$/i,"a"); if(aCity[parseInt((0,2))]==null) return "Your ID card is illegal"; sBirthday=(6,4)+"-"+Number((10,2))+"-"+Number((12,2)); var d=new Date((/-/g,"/")) ; if(sBirthday!=(()+"-"+ (()+1) + "-" + ()))return "Date of birth on ID card is illegal"; for(var i = 17;i>=0;i --) iSum += ((2,i) % 11) * parseInt((17 - i),11) ; if(iSum%11!=1) return "The ID number you entered is illegal"; //aCity[parseInt((0,2))]+","+sBirthday+","+((16,1)%2?"Male":"Female");//This time you can also determine the gender of the entered ID number. return true; }
Summarize
The above is the method of encrypting the ID card number by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!