Simple example of js verifying real name ID number and mobile phone number

  • 2021-07-04 17:42:43
  • OfStack

The interface used in recent projects needs to call real-name authentication. Compared with SMS, the price of real-name authentication interface is not a few cents higher. Therefore, the conditions for calling real-name authentication should be strictly controlled. Therefore, js is used to verify the real name and js to verify the ID number.

Get down to business

1. js Verify Real Name

js verifies the real name, is uses the unicode character to carry on the match, but the Chinese name length 1 generally is 2-4, therefore duplicates matches {2, 4} times


 var regName =/^[\u4e00-\u9fa5]{2,4}$/;
 if(!regName.test(name)){
   alert(' The real name is filled in incorrectly ');
   return false;
 }

2. js verification ID number

js verifies the ID number, China's ID number, the first generation ID number is 15 digits, and the second generation ID card is all 18 digits. The last check digit may be 'X' or 'x', so there are four possibilities: a. 15 digits, b. 18 digits, c. 17 digits, 108th digit is' X ', d. 17 digits, and 108th digit is' x '


var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
 if(!regIdNo.test(idNo)){
   alert(' The ID number is filled in incorrectly ');
   return false;
 }

Detailed ID card verification:

https://www.ofstack.com/article/88771.htm

3. js verifies the mobile phone number

In addition to the area code (+86), the mobile phone numbers in China are all 11 digits and the first letter 1 is 1, but the second digit is not 1, but so far there is no 1 and 2


var mobileRegex = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/;
 
     if(mobileRegex.test(phone)){
       alert(' The mobile phone number is correct ');
     }else{
       alert(' The mobile phone number is incorrectly entered ');
     }

Related articles: