Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript regular expression to verify whether the ID card number is legal

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use JavaScript regular expression to verify whether the ID card number is legal". In the daily operation, I believe that many people have doubts about how to use JavaScript regular expression to verify whether the ID card number is legal. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to use JavaScript regular expression to verify whether the ID card number is legal or not." Next, please follow the editor to study!

The first method:

In the user registration page, some requirements are relatively strict, need to verify whether the ID card js is legal, through this function strictly this system software, thus filtering to a lot of water passengers. The following implementation method to explain to you.

Most of the time, we use a set of regular expressions to determine whether the ID card entered by the user is legal, so how much do you know about the composition of the ID number before using the regular expression? Here's how much information is contained in an ID number:

1. The structure of the number

The citizenship number is a feature combination code, which is composed of a 17-digit ontology code and an one-digit check code. The order from left to right is: six-digit address code, eight-digit birthday code, three-digit sequence code and one-digit check code.

2. Address code (first six digits)

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

3. Date of birth code (7th to 14th)

Indicates the year, month and day of the birth of the coding object, which is carried out in accordance with the provisions of gb/T7408, and there is no delimiter between the code of year, month and day.

4. Sequence code (15th to 17th digits)

Indicates that within the area identified by the same address code, the odd number of the sequence code is assigned to men and even numbers to women in the same year, month and day.

5. Check code (eighteenth digit)

As the check code of the tail number, it is calculated by the number compilation unit according to the unified formula. If someone's tail number is 0mer9, there will be no X, but if the tail number is 10, then it must be replaced by X, because if 10 is used as the tail number, then the person's ID card becomes 19 digits. X is the Roman numeral 10, using X instead of 10, can ensure that citizens' identity cards meet national standards.

After knowing the meaning of the various parts of the ID card number structure, we began to move on to the topic:

1. Define the object of a national region

Var aCity= {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: Xizang, 61: Shaanxi, 62: Gansu, 63: Qinghai, 64: Ningxia, 65: Xinjiang, 71: Taiwan, 81: Hong Kong, 82: Macao, 91: abroad}

2. Regular expression judgment

Function isCardID (sId) {var iSum=0; var info= ""; if (! / ^\ d {17} (\ d | x) $/ i.test (sId)) return "the length or format of your ID card you entered is incorrect"; sId=sId.replace (/ x ID tag I, "a"); if (aCit [parseInt (sId.substr (0jue 2))] = null) return "your ID card area is illegal"; sBirthday=sId.substr (6) + Number (sId.substr (10)) + "-" + Number (sId.substr (12)) Var d=new Date (sBirthday.replace (/-/ g, "/")); if (Birthdaytime = (d.getFullYear () + "-" + (d.getMonth () + 1) + "- + d.getDate ()) return" illegal date of birth on ID card "; for (var I = 17 var I > = 0 iSum I--) iSum + = (Math.pow (2m I)% 11) * parseInt (sId.charAt (17-I), 11) If (iSummer1) return "the ID number you entered is illegal"; / / aCity [parseInt (sId.substr (0meme 2))] + "," + sBirthday+ "," + (sId.substr (16meme 1)% 2? " "male": "female"); / / you can also determine the gender of the ID number entered this time return true;}

The second method:

Function scCard () {var scType=document.getElementById ("sc_card_type"). Value; if (scType== "1") {var scCard=document.getElementById ("sc_card_num"). Value; if (scCard.roomthread0) {if (! checkCard (scCard)) {$("# errorTips"). Html ("ID card number format error");} else {$("# errorTips"). Html ("") } return false / / function checkidno (obj) {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: Xizang, 61: Shaanxi, 62: Gansu, 63: Qinghai, 64: Ningxia, 65: Xinjiang, 71: Taiwan, 81: Hong Kong. 82: "Macao", 91: "abroad"} CheckCard = function (obj) {/ / var card = document.getElementById ('card_no'). Whether value; / / is empty / / if (card = ='') / / {/ / return false; / /} / / check length, type if (isCardNo (obj) = false) {return false;} / / check province if (checkProvince (obj) = false) {return false } / / check birthday if (checkBirthday (obj) = false) {return false;} / / check bit detection if (checkParity (obj) = false) {return false;} return true;} / / check whether the number conforms to the specification, including length, type isCardNo = function (obj) {/ / ID number is 15 or 18 digits, 15 digits are all digits, the first 17 digits of the 18 digits are digits, and the last digit is the check bit, which may be a number or character X var reg = / (^\ d {15} $) | (^\ d {17} (\ d | X) $) /; if (reg.test (obj) = false) {return false } return true;}; / / take the first two digits of the ID card, and verify that the province checkProvince = function (obj) {var province = obj.substr (0meme 2); if (vcity[ return false;] = = undefined) {return false;} return true;}; / / check whether the birthday is correct checkBirthday = function (obj) {var len = obj.length / / when the ID card is 15 digits, the order is province (3 digits), city (3 digits), year (2 digits), month (2 digits), day (2 digits), check bits (3 digits), all digital if (len = = '15') {var re_fifteen = / ^ (\ d {6}) (\ d {2}) (\ d {3}) $/; var arr_data = obj.match (re_fifteen) Var year = arr_data [2]; var month = arr_data [3]; var day = arr_data [4]; var birthday = new Date ('19'+year+'/'+month+'/'+day); return verifyBirthday } / / when the ID card is 18 digits, the order is province (3 digits), city (3 digits), year (4 digits), month (2 digits), day (2 digits), check bits (4 digits), and the end of the check bits may be X if (len = = '18') {var re_eighteen = / ^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2}) (\ d {3}) ([0-9] | X) $/ Var arr_data = obj.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;}; / / check date verifyBirthday = function (year,month,day,birthday) {var now = new Date () Var now_year = now.getFullYear (); / / whether the month and day of the year is reasonable if (birthday.getFullYear () = = year & & (birthday.getMonth () + 1) = month & & birthday.getDate () = = day) {/ / the range of years (between 3 and 100 years old) var time = now_year-year; if (time > = 0 & & time

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report