start
Verify the real name information of users, that is, to determine whether the ID number, name is correct or not.
There are two broad categories of methods:
1. Free manual verification
The name ID number name can only be verified by regular verification, and the ID number can not be matched with the ID number, but the advantage is free. Suitable for start-up teams or projects with tolerable errors in real name information.
2. Toll api interface
That is, we send these ID number and name to these api interface sites, and verify them and give us accurate results. But the disadvantage is that it is not cheap. Some service providers charge only if their ID cards match in a single verification. If they don't match, they don't charge. Such service providers are quite conscientious.
1, Free manual verification code
Be sure to know the principle before writing the code:
At present, we use 18 second-generation ID cards, including
1-6 digit place of birth code (pure number);
7-10 digit year of birth (pure number);
11-12 birth months (pure numbers);
13-14 digit date of birth (pure number);
15-16 digit birth order number (pure number);
17 digit gender label (pure number, odd for men and even for women);
18 digit validation code (may be number or letter X);
Calibration principle:
According to the first 17 digit code, according to iso7064:1983 The MOD11-2 check code calculates eighteenth digit test codes. The popular saying is that the last digit of the ID number is calculated from the top 17.
Therefore, the way to verify the ID number by hand is to prevent those who are trying to pass customs. They can only act as a testing tool to prevent the wrong transfer.
ISO7064:1983.MOD11-2 Verification method 1,The 17 numbers of the front ID number are multiplied by different coefficients. The coefficients from the first to the seventeenth are 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2. 2,Add the result of multiplying these 17 digits by the coefficient. 3,If you add it up and divide by 11, the remainder can only be 0-1-2-3-4-5-6-7-8-9-10 These 11 numbers. The number of the last ID card corresponding to it is 1-0-X -9-8-7-6-5-4-3-2. 4,According to the above, if the remainder is 3, 9 will appear in the 18th digit of the ID card. If the corresponding number is 2, the last number of the ID card is a Roman number X.
js code (including other toolsets):
//ID card tools var idcardUtil = function () { //Check the ID number is correct, return to true/false var verify = function (idcard) { //Verify whether the first two digits are correct var provinces = '#11#12#13#14#15#21#22#23#31#32#33#34#35#36#37#41#42#43#44#45#46#50#51#52#53#54#61#62#63#64#65#71#81#82#'; var province = idcard.substr(0, 2); if (provinces.indexOf(province) == -1) {//The first two are provinces return false; } //Verify that the format of the birthday date is correct (avoid errors such as February 31) var birthday_year = idcard.substr(6, 4); var nowYear = new Date().getFullYear(); if (Number(birthday_year) < 1900 || Number(birthday_year) > nowYear) { return false; } var birthday_month = idcard.substr(10, 2); if (Number(birthday_month) > 12 || Number(birthday_month) < 1) { return false; } var birthday_date = idcard.substr(12, 2); var isLeapYear = false; if (birthday_year % 100 == 0 && birthday_year % 400 == 0) {//Century year isLeapYear = true; } else if (birthday_year % 4 == 0 && birthday_year % 100 != 0) {//Ordinary year isLeapYear = true; } var month31 = '#1#3#5#7#8#10#12#';// 31 day month if (birthday_month == 2) { if (isLeapYear && birthday_date >= 29) { return false; } if (!isLeapYear && birthday_date >= 28) { return false; } } if (month31.indexOf(Number(birthday_month)) == -1 && birthday_month == 31) { return false; } if (Number(birthday_date) > 31 || Number(birthday_date) < 1) { return false; } //Start calculation of check code var x17 = 0; var ratios = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var result = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; for (var n in idcard) { if (n <= 16) { x17 += Number(idcard[n]) * ratios[n]; } } if (idcard[17] != result[x17 % 11]) { //Calculate remainder return false; } return true; }; //According to the ID number, gender was calculated, and the male returned 1 and the female returned 0. var getGender = function (idcard) { var num = idcard.substring(16, 17); return (num % 2 == 0) ? '0' : '1'; }; //Calculate age according to ID number var getAge = function (idcard) { var birthdayStr = idcard.substring(6, 10) + '-' + idcard.substring(10, 12) + '-' + idcard.substring(12, 14); var returnAge; var strBirthdayArr = birthdayStr.split("-"); var birthYear = strBirthdayArr[0]; var birthMonth = strBirthdayArr[1]; var birthDay = strBirthdayArr[2]; d = new Date(); var nowYear = d.getFullYear(); var nowMonth = d.getMonth() + 1; var nowDay = d.getDate(); if (nowYear == birthYear) { returnAge = 0; // The same year is 0 years old } else { var ageDiff = nowYear - birthYear; // Year difference if (ageDiff > 0) { if (nowMonth == birthMonth) { var dayDiff = nowDay - birthDay; // Day difference if (dayDiff < 0) { returnAge = ageDiff - 1; } else { returnAge = ageDiff; } } else { var monthDiff = nowMonth - birthMonth; // Month difference if (monthDiff < 0) { returnAge = ageDiff - 1; } else { returnAge = ageDiff; } } } else { returnAge = -1; // Returning - 1 means that the birth date is entered incorrectly later than today } } return returnAge; // Return to one year of age }; //Get the date of birth in the ID number. var getBirthday = function (idcard) { var birthday_year = idcard.substr(6, 4); var birthday_month = idcard.substr(10, 2); var birthday_date = idcard.substr(12, 2); return birthday_year + '-' + birthday_month + '-' + birthday_date; }; return {verify: verify, getGender: getGender, getAge: getAge, getBirthday: getBirthday}; }();
2, Charging API interface verification
Commonly used are Alibaba cloud,Aggregate interface.
These are charging interfaces. The more people who can play their products, the more money they make.
Therefore, when you go to the official website, there are very detailed development documents. When you encounter a problem, you can call customer service in seconds. It is very easy to integrate into the code, so I won't repeat it here.
But here are some optimization suggestions:
- First perform the local verification in the first step, and then upload the third-party api interface for verification, which can avoid the consumption caused by some users' filling errors and save costs.
- The user information after each successful real name can be stored in the company's special user real name information database. When all projects need real name authentication in the future, first verify locally, and then search the company's user real name database. If it is not found, then use the third-party interface for verification. In this way, when the number of users becomes large, it will reduce many user costs. (major premise: it needs to be written in the user's code for the user's consent, and if the company is legally responsible for data leakage, in short, it needs to read the law in detail)
-end
Please correct any mistakes, leave a message if you have any questions, thank you