search
HomeWeb Front-endJS Tutorialjs verifies whether the real name matches the ID number_javascript skills

In recent projects, an interface needs to be called for real-name authentication. The price of the real-name authentication interface is 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 authenticity. Name and js verification ID number.

Get to the point

js verifies the real name and uses unicode characters for matching. The length of Chinese names is generally 2-4, so the match is repeated {2, 4} times

1.js verify real name

 var regName =/^[\ue-\ufa]{,}$/;
 if(!regName.test(name)){
  alert('真实姓名填写有误');
  return false;
 }

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

2.js verification of ID number

 var regIdNo = /(^\d{}$)|(^\d{}$)|(^\d{}(\d|X|x)$)/;
 if(!regIdNo.test(idNo)){
  alert('身份证号填写有误');
  return false;
 }

PS:15位和18位身份证JS校验实例

一、身份证号码的结构和表示形式

1、号码的结构

根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。

2、地址码

表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。

3、出生日期码

表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。

4、顺序码

表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。

5、校验码

(1)十七位数字本体码加权求和公式

S = Sum(Ai * Wi), i = 0, ... , 16 ,      先对前17位数字的权求和
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模

Y = mod(S, 11)

(3)通过模得到对应的校验码

Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2
也就是说,如果得到余数为1则最后的校验位p应该为对应的0。

15位的号码:

a a b b c c y y m m d d x x s

18位的号码:

a a b b c c y y y y m m d d x x s p

二、地址码

身份证前6位为行政区划数字代码(简称数字码)说明(参考《GB/T 2260-2007 中华人民共和国行政区划代码》):该数字码的编制原则和结构分析,它采用三层六位层次码结构,按层次分别表示我国各省(自治区,直辖市,特别行政区)、市(地区,自治州,盟)、县(自治县、县级市、旗、自治旗、市辖区、林区、特区)。

(1)第1、2位数字:所在省份编码,表示省、自治区、直辖市、特别行政区。
      省,直辖市代码表: { 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:"澳门",91:"国外"} 

(2)第3、4位数字:所在省级市(县)编码,表示市、地区、自治州、盟、直辖市所辖市辖区、县汇总码、省(自治区)直辖县级行政区划汇总码。
——01~20、51~70表示市,直辖市则01表示市区,02以后表示直辖市辖区内的郊县;
——21~50表示地区、自治州、盟;
——90表示省(自治区)直辖县级行政区划汇总码。

(3)第5、6位数字:所在地级县(市)编码,表示县、自治县、县级市、旗、自治旗、市辖区、林区、特区。
——01~20表示市辖区、地区(自治州、盟)辖县级市、市辖特区以及省(自治区)直辖县级行政区划中的县级市,01通常表示辖区汇总码;
——21~80表示县、自治县、旗、自治旗、林区、地区辖特区;
——81~99表示省(自治区)辖县级市。

三、其它

15位身份证号码:
第7、8位为出生年份(两位数,用年份的后两位表示),
第9、10位为出生月份,
第11、12位代表出生日期,
第13、14位为顺序码,这个是随机数,
第15位代表性别,奇数为男,偶数为女。
18位身份证号码:
第7、8、9、10位为出生年份(四位数),
第11、12位为出生月份,
第13、14位代表出生日期,
第15、16位为顺序码,
第17位代表性别,奇数为男,偶数为女。
最后一位为校验位。

四、Js代码实例

var idCardNoUtil = {
 /*省,直辖市代码表*/
 provinceAndCitys: {:"北京",:"天津",:"河北",:"山西",:"内蒙古",:"辽宁",:"吉林",:"黑龙江",
 :"上海",:"江苏",:"浙江",:"安徽",:"福建",:"江西",:"山东",:"河南",:"湖北",:"湖南",:"广东",
 :"广西",:"海南",:"重庆",:"四川",:"贵州",:"云南",:"西藏",:"陕西",:"甘肃",:"青海",:"宁夏",
 :"新疆",:"台湾",:"香港",:"澳门",:"国外"},
 /*每位加权因子*/
 powers: ["","","","","","","","","","","","","","","","",""],
 /*第位校检码*/
 parityBit: ["","","X","","","","","","","",""],
 /*性别*/
 genders: {male:"男",female:"女"},
 /*校验地址码*/
 checkAddressCode: function(addressCode){
  var check = /^[-]\d{}$/.test(addressCode);
  if(!check) return false;
  if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(,))]){
  return true;
  }else{
  return false;
  }
 },
 /*校验日期码*/
 checkBirthDayCode: function(birDayCode){
  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))$/.test(birDayCode);
  if(!check) return false; 
  var yyyy = parseInt(birDayCode.substring(,),);
  var mm = parseInt(birDayCode.substring(,),);
  var dd = parseInt(birDayCode.substring(),);
 var xdata = new Date(yyyy,mm-,dd);
 if(xdata > new Date()){
  return false;//生日不能大于当前日期
 }else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - ) && ( xdata.getDate() == dd ) ){
  return true;
 }else{
  return false;
 }
 },
 /*计算校检码*/
 getParityBit: function(idCardNo){
 var id = idCardNo.substring(,); 
  /*加权 */
  var power = ;
  for(var i=;i<;i++){
  power += parseInt(id.charAt(i),) * parseInt(idCardNoUtil.powers[i]);
  }    
  /*取模*/ 
  var mod = power % ;
  return idCardNoUtil.parityBit[mod];
 },
 /*验证校检码*/
 checkParityBit: function(idCardNo){
  var parityBit = idCardNo.charAt().toUpperCase();
  if(idCardNoUtil.getParityBit(idCardNo) == parityBit){
   return true;
  }else{
   return false;
  }
 },
 /*校验位或位的身份证号码*/
 checkIdCardNo: function(idCardNo){
 //位和位身份证号码的基本校验
 var check = /^\d{}|(\d{}(\d|x|X))$/.test(idCardNo);
 if(!check) return false;
 //判断长度为位或位 
 if(idCardNo.length==){
  return idCardNoUtil.checkIdCardNo(idCardNo);
 }else if(idCardNo.length==){
  return idCardNoUtil.checkIdCardNo(idCardNo);
 }else{
  return false;
 }
 },
 //校验位的身份证号码
 checkIdCardNo: function(idCardNo){
  //位身份证号码的基本校验
  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}$/.test(idCardNo); 
  if(!check) return false;
  //校验地址码
  var addressCode = idCardNo.substring(,);
  check = idCardNoUtil.checkAddressCode(addressCode);
  if(!check) return false;
  var birDayCode = '' + idCardNo.substring(,);
  //校验日期码
  return idCardNoUtil.checkBirthDayCode(birDayCode);
 },
 //校验位的身份证号码
 checkIdCardNo: function(idCardNo){
  //位身份证号码的基本格式校验
  var check = /^[-]\d{}[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}(\d|x|X)$/.test(idCardNo);
  if(!check) return false;
  //校验地址码
  var addressCode = idCardNo.substring(,);
  check = idCardNoUtil.checkAddressCode(addressCode);
  if(!check) return false;
  //校验日期码
  var birDayCode = idCardNo.substring(,);
  check = idCardNoUtil.checkBirthDayCode(birDayCode);
  if(!check) return false;
  //验证校检码 
  return idCardNoUtil.checkParityBit(idCardNo); 
 },
 formateDateCN: function(day){
  var yyyy =day.substring(,);
  var mm = day.substring(,);
  var dd = day.substring();
  return yyyy + '-' + mm +'-' + dd;
 },
 //获取信息
 getIdCardInfo: function(idCardNo){
  var idCardInfo = {
  gender:"", //性别
  birthday:"" // 出生日期(yyyy-mm-dd)
  };
 if(idCardNo.length==){
  var aday = '' + idCardNo.substring(,);
  idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
  if(parseInt(idCardNo.charAt())%==){
   idCardInfo.gender=idCardNoUtil.genders.female;
  }else{
   idCardInfo.gender=idCardNoUtil.genders.male;
  }  
 }else if(idCardNo.length==){
  var aday = idCardNo.substring(,);
  idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
   if(parseInt(idCardNo.charAt())%==){
   idCardInfo.gender=idCardNoUtil.genders.female;
  }else{
   idCardInfo.gender=idCardNoUtil.genders.male;
  } 
 }
 return idCardInfo;
 },
 /*位转位*/
 getId: function(idCardNo){
 if(idCardNo.length==){
  return idCardNo;
 }else if(idCardNo.length==){
  return idCardNo.substring(,) + idCardNo.substring(,); 
 }else{
 return null;
 }
 },
 /*位转位*/
 getId: function(idCardNo){
 if(idCardNo.length==){
  var id = idCardNo.substring(,) + '' + idCardNo.substring();
  var parityBit = idCardNoUtil.getParityBit(id);
  return id + parityBit;
 }else if(idCardNo.length==){
  return idCardNo;
 }else{
 return null;
 }
 }
};
//身份证号码验证 
jQuery.validator.addMethod("idCardNo", function(value, element) { 
 return this.optional(element) || idCardNoUtil.checkIdCardNo(value); 
}, "Please specify a valid ID number."); 
//获取身份证信息 
var idCardInfo = idCardNoUtil.getIdCardInfo(idCardNo); 
alert(idCardInfo.gender + "|" + idCardInfo.birthday); 

Note: When entering and judging whether the same ID card already exists in the database

(1) If you enter a 15-digit ID card: first check whether the 15-digit ID exists. If it does not exist, you need to convert the 15-digit ID card into an 18-digit ID card. If it still does not exist, you can Enter the system.

(2) If you enter an 18-digit ID card: first check whether the 18-digit ID exists. If it does not exist, you need to convert the 18-digit ID card into a 15-digit ID card. If it still does not exist, you can Enter the system.

If you find the corresponding 15-digit ID card, you need to update the 15-digit ID to 18-digit.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function