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
JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools