SoFunction
Updated on 2025-02-28

JS uses regular expressions to verify ID number

I won't say much nonsense, let's show you a piece of code first

function isCardNo(card) 
{ 
  // The ID number is 15 or 18 digits. When 15 digits, it is all numbers. The first 17 digits of 18 digits are numbers. The last digit is a check digit, which may be a number or character X  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
  if((card) === false) 
  { 
    alert("Illegal card input"); 
    return false; 
  } 
} 

The interface used in recent projects requires calling real-name authentication. The price of real-name authentication interface is not a few cents higher than that of SMS. Therefore, the conditions for calling real-name authentication must be strictly controlled, so JS is used to verify the real name and JS is used to verify the ID number.

Go to the main topic

JS verification of real names is used to match unicode characters, while Chinese names are generally 2-4, so the match is repeated {2,4} times

Verify the real name

var regName =/^[\u4e00-\u9fa5]{2,4}$/; 
if(!(name)){ 
  alert(‘Fill incorrectly with real name‘); 
   return false; 
 } 

js verification ID number, Chinese ID number, first generation ID number is a 15-digit number, second generation ID number is all 18-digit, and the last check digit may be a digit or 'x', so there are four possibilities: a.15-digit number b.18-digit number c.17-digit number, the eighteenth digit is ‘X’ d.17-digit number, and the eighteenth digit is ‘x’

Verify ID number

var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
if(!(idNo)){ 
  alert(‘Is the ID number filled in incorrectly‘); 
  return false; 
} 

The above is the JS verification ID number using regular expressions introduced 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!