


Specific implementation method of ultra-accurate JavaScript verification of ID number_javascript skills
Before writing the program, let’s first understand the composition of the ID number:
There are two types of ID numbers, the old one is 15 digits and the new one is 18 digits.
ID card 15-digit encoding rule: dddddd yymmdd xx p
Among them dddddd: area code yymmdd: date of birth xx: sequential code, cannot be determined p: gender, odd numbers are male, even numbers are female;
ID card 18-bit encoding rule: dddddd yyyymmdd xxx y
Among them dddddd: area code yyyymmdd: year, month and day of birth xxx: sequential code, cannot be determined, odd numbers are male, even numbers are female y: Check code, the value of this digit can be obtained by calculating the first 17 digits, the calculation formula See the program, Some constants that need to be used :
The weighting factor of the 18-digit number is (from right to left) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ] Verification bit Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] Check digit calculation formula: Y_P = mod( ∑(Ai×Wi),11 )
i is the 2...18 digits of the ID card number from right to left; Y_P is the position in the check code array where the foot check code is located.
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子 var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]; // 身份证验证位值.10代表X function IdCardValidate(idCard) { idCard = trim(idCard.replace(/ /g, "")); //去掉字符串头尾空格 if (idCard.length == 15) { return isValidityBrithBy15IdCard(idCard); //进行15位身份证的验证 } else if (idCard.length == 18) { var a_idCard = idCard.split(""); // 得到身份证数组 if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){ //进行18位身份证的基本验证和第18位的验证 return true; }else { return false; } } else { return false; } } /** * 判断身份证号码为18位时最后的验证位是否正确 * @param a_idCard 身份证号码数组 * @return */ function isTrueValidateCodeBy18IdCard(a_idCard) { var sum = 0; // 声明加权求和变量 if (a_idCard[17].toLowerCase() == 'x') { a_idCard[17] = 10; // 将最后位为x的验证码替换为10方便后续操作 } for ( var i = 0; i < 17; i++) { sum += Wi[i] * a_idCard[i]; // 加权求和 } valCodePosition = sum % 11; // 得到验证码所位置 if (a_idCard[17] == ValideCode[valCodePosition]) { return true; } else { return false; } } /** * 验证18位数身份证号码中的生日是否是有效生日 * @param idCard 18位书身份证字符串 * @return */ function isValidityBrithBy18IdCard(idCard18){ var year = idCard18.substring(6,10); var month = idCard18.substring(10,12); var day = idCard18.substring(12,14); var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day)); // 这里用getFullYear()获取年份,避免千年虫问题 if(temp_date.getFullYear()!=parseFloat(year) ||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)){ return false; }else{ return true; } } /** * 验证15位数身份证号码中的生日是否是有效生日 * @param idCard15 15位书身份证字符串 * @return */ function isValidityBrithBy15IdCard(idCard15){ var year = idCard15.substring(6,8); var month = idCard15.substring(8,10); var day = idCard15.substring(10,12); var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day)); // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法 if(temp_date.getYear()!=parseFloat(year) ||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)){ return false; }else{ return true; } } //去掉字符串头尾空格 function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); }
Determine gender based on ID number
/** * 通过身份证判断是男是女 * @param idCard 15/18位身份证号码 * @return 'female'-女、'male'-男 */ function maleOrFemalByIdCard(idCard){ idCard = trim(idCard.replace(/ /g, "")); // 对身份证号码做处理。包括字符间有空格。 if(idCard.length==15){ if(idCard.substring(14,15)%2==0){ return 'female'; }else{ return 'male'; } }else if(idCard.length ==18){ if(idCard.substring(14,17)%2==0){ return 'female'; }else{ return 'male'; } }else{ return null; } }
The above is the specific implementation method of javascript to verify the ID number, and the gender can be verified based on the ID card. Isn’t it amazing? Everyone is welcome to learn from it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
