Table of Contents
Phone verification" >Phone verification
Number Verification" >Number Verification
License Plate Number Verification" >License Plate Number Verification
ID card number verification includes 15-digit and 18-digit ID card verification" >ID card number verification includes 15-digit and 18-digit ID card verification
Home Web Front-end JS Tutorial A collection of commonly used regular expressions in js

A collection of commonly used regular expressions in js

Feb 22, 2017 pm 01:28 PM

The following contains some regular expressions that I often use, because almost all the places where regular expressions are used in work scenarios are related to the verification of the validate plug-in,

So The following regular rules are also an extension of $.validator.addMethod():

validate: http://www.php.cn/

Phone verification

/**
 * 手机
 * */
$.validator.addMethod("isMobile", function (value, element) {
    var reg = /^((1[3-8][0-9])+\d{8})$/;
    return this.optional(element) || (reg.test(value));
}, "手机格式不正确");
/**
 * 号码,固话与手机都可以
 * */
jQuery.validator.addMethod("allPhone", function(v, e) {
        return this.optional(e) || /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(v)||/^(1(([35][0-9])|(47)|[8][01236789]))\d{8}$/.test(v);},
    "请输入正确的号码:区号-电话号码/手机号");
/**
 * 固话、传真,传真格式与固话是一样的
 * */
jQuery.validator.addMethod("isTel", function(v, e) { return this.optional(e) || /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(v);}, "请输入正确的电话号码");
/**
 * 手机
 * */
$.validator.addMethod("isMobile", function (value, element) {
    var reg = /^((1[3-8][0-9])+\d{8})$/;
    return this.optional(element) || (reg.test(value));
}, "手机格式不正确");

/**
 * 号码,固话与手机都可以
 * */
jQuery.validator.addMethod("allPhone", function(v, e) {
        return this.optional(e) || /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(v)||/^(1(([35][0-9])|(47)|[8][01236789]))\d{8}$/.test(v);},
    "请输入正确的号码:区号-电话号码/手机号");

/**
 * 固话、传真,传真格式与固话是一样的
 * */
jQuery.validator.addMethod("isTel", function(v, e) { return this.optional(e) || /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(v);}, "请输入正确的电话号码");
Copy after login

Number Verification

/**
 * 正整数
 * */
$.validator.addMethod("ispositivenum", function (value, element) {
    var reg = /^([0]|[1-9]\d*)$/;
    return this.optional(element) || (reg.test(value));
}, "请输入正整数");
/**
 * 正数(包括浮点数)
 * */
$.validator.addMethod("pFloatTwo", function (value, element) {
    return this.optional(element) || (/^([1-9]\d*|[0])(\.\d{1,2})?$/.test(value));
}, "请输入正数,最多保留两位小数");
/**
 * 价格(包括浮点数)最大值99999.99
 * */
$.validator.addMethod("price", function (value, element) {
    return this.optional(element) || (value>0&&(/^([1-9]\d{0,4}|[0])(\.\d{1,2})?$/.test(value)));
}, "请输入正数,最大值99999.99,最多保留两位小数");
/**
 * 规格
 * */
$.validator.addMethod("size", function (value, element) {
    var reg = /^[1-9]\d{0,4}$/;
    return this.optional(element) || (reg.test(value));
}, "请输入正整数,最大值99999");
/**
 * 数量
 * */
$.validator.addMethod("qty", function (value, element) {
    return this.optional(element) || (value>0 && (/^([1-9]\d{0,4}|[0])(\.\d{1,3})?$/.test(value)));
}, "请输入正数,最大值99999.999,最多保留三位小数");




/**
 * 正整数
 * */
$.validator.addMethod("ispositivenum", function (value, element) {
    var reg = /^([0]|[1-9]\d*)$/;
    return this.optional(element) || (reg.test(value));
}, "请输入正整数");

/**
 * 正数(包括浮点数)
 * */
$.validator.addMethod("pFloatTwo", function (value, element) {
    return this.optional(element) || (/^([1-9]\d*|[0])(\.\d{1,2})?$/.test(value));
}, "请输入正数,最多保留两位小数");

/**
 * 价格(包括浮点数)最大值99999.99
 * */
$.validator.addMethod("price", function (value, element) {
    return this.optional(element) || (value>0&&(/^([1-9]\d{0,4}|[0])(\.\d{1,2})?$/.test(value)));
}, "请输入正数,最大值99999.99,最多保留两位小数");

/**
 * 规格
 * */
$.validator.addMethod("size", function (value, element) {
    var reg = /^[1-9]\d{0,4}$/;
    return this.optional(element) || (reg.test(value));
}, "请输入正整数,最大值99999");
/**
 * 数量
 * */
$.validator.addMethod("qty", function (value, element) {
    return this.optional(element) || (value>0 && (/^([1-9]\d{0,4}|[0])(\.\d{1,3})?$/.test(value)));
}, "请输入正数,最大值99999.999,最多保留三位小数");
Copy after login


License Plate Number Verification

/**
 * 车牌号码验证
 * */
$.validator.addMethod("isCarNo", function(value, element){
    var reg = /^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$|^[a-zA-Z]{2}\d{7}$ /;
    return this.optional(element) || (reg.test(value));
},"请输入正确的车牌号码,大小写不区分");




/**
 * 车牌号码验证
 * */
$.validator.addMethod("isCarNo", function(value, element){
    var reg = /^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$|^[a-zA-Z]{2}\d{7}$ /;
    return this.optional(element) || (reg.test(value));
},"请输入正确的车牌号码,大小写不区分");
Copy after login


ID card number verification includes 15-digit and 18-digit ID card verification

//身份证15位转18位中,计算校验位即最后一位
function GetVerifyBit(id){
    var result;
    var nNum=eval(id.charAt(0)*7+id.charAt(1)*9+id.charAt(2)*10+id.charAt(3)*5+id.charAt(4)*8+id.charAt(5)*4+id.charAt(6)*2+id.charAt(7)*1+id.charAt(8)*6+id.
    charAt(9)*3+id.charAt(10)*7+id.charAt(11)*9+id.charAt(12)*10+id.charAt(13)*5+id.charAt(14)*8+id.charAt(15)*4+id.charAt(16)*2);
    nNum=nNum%11;
    switch (nNum) {
        case 0 :
            result="1";
            break;
        case 1 :
            result="0";
            break;
        case 2 :
            result="X";
            break;
        case 3 :
            result="9";
            break;
        case 4 :
            result="8";
            break;
        case 5 :
            result="7";
            break;
        case 6 :
            result="6";
            break;
        case 7 :
            result="5";
            break;
        case 8 :
            result="4";
            break;
        case 9 :
            result="3";
            break;
        case 10 :
            result="2";
            break;
    }
    //document.write(result);
    return result;
}
/*
 功能:验证身份证号码是否有效
 提 示信息:未输入或输入身份证号不正确!
 使用:validateIdCard(obj,birthday,s)//s:1为男,0为女
 返回:0,1,2,3,4,5
 */
function validateIdCard(obj,birthday,s){
    var aCity={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:"国 外"};
    var iSum = 0;
    //var info = "";
    var strIDno = obj;
    if(birthday!=null){
        birthday = birthday.replace(/-/g,"/");
    }
    var idCardLength = strIDno.length;
    if(!/^\d{17}(\d|x)$/i.test(strIDno)&&!/^\d{15}$/i.test(strIDno)){
        return 1; //非法身份证号
    }
    if(aCity[parseInt(strIDno.substr(0,2))]==null){
        return 2;// 非法地区
    }
    // 15位身份证转换为18位
    if (idCardLength==15){
        sBirthday = "19" + strIDno.substr(6,2) + "-" + Number(strIDno.substr(8,2)) + "-" + Number(strIDno.substr(10,2));
        var d = new Date(sBirthday.replace(/-/g,"/"));
        var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();
        var genderNo=strIDno.substr(14,1);
        if(sBirthday != dd){
            return 3; //非法生日
        }
        if(birthday==""){
            return 4;//您还没填写出生日期
        }
        if(birthday!=null && d.getTime()!=new Date(birthday).getTime()){
            return 5; //与出生日期不符
        }
        if(s!=null && s!=0 && s!=1){
            return 6;//您还没填写性别
        }
        if(s!=null && genderNo%2!=s){
            return 7;//与性别不符
        }
        strIDno=strIDno.substring(0,6)+"19"+strIDno.substring(6,15);
        strIDno=strIDno+GetVerifyBit(strIDno);
    }
    // 判断是否大于2078年,小于1900年
    var year =strIDno.substring(6,10);
    if (year<1900 || year>2078 ){
        return 3;//非法生日
    }
    /*if(){
     }*/
    //18位身份证处理
    //在后面的运算中x相当于数字10,所以转换成a
    strIDno = strIDno.replace(/x$/i,"a");
    sBirthday=strIDno.substr(6,4)+"-"+Number(strIDno.substr(10,2))+"-"+Number(strIDno.substr(12,2));
    var d = new Date(sBirthday.replace(/-/g,"/"));
    var genderNo=strIDno.substr(16,1);
    if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
        return 3; //非法生日
    }
    if(birthday==""){
        return 4;//您还没填写出生日期
    }
    if(birthday!=null && d.getTime()!=new Date(birthday).getTime()){
        return 5; //与出生日期不符
    }
    if(s!=null && s!=0 && s!=1){
        return 6;//您还没填写性别
    }
    if(s!=null && genderNo%2!=s){
        return 7;//与性别不符
    }
    // 身份证编码规范验证
    for(var i = 17;i>=0;i --){
        iSum += (Math.pow(2,i) % 11) * parseInt(strIDno.charAt(17 - i),11);
    }
    if(iSum%11!=1){
        return 1;// 非法身份证号
    }
    // 判断是否屏蔽身份证
    var words = new Array();
    words = new Array("11111119111111111","12121219121212121");
    for(var k=0;k<words.length;k++){
        if (strIDno.indexOf(words[k])!=-1){
            return 1;
        }
    }
    return 0;
}
//身份证(无关联验证)
$.validator.addMethod("cretID", function(value, element, param){
    var n=validateIdCard(value);
    var error=["","非法身份证号","地区编号不合法","出生日期不合法"];
    param[1]=error[n];
    return this.optional(element) || n==0;
},$.validator.format("{1}"));




//身份证15位转18位中,计算校验位即最后一位
function GetVerifyBit(id){
    var result;
    var nNum=eval(id.charAt(0)*7+id.charAt(1)*9+id.charAt(2)*10+id.charAt(3)*5+id.charAt(4)*8+id.charAt(5)*4+id.charAt(6)*2+id.charAt(7)*1+id.charAt(8)*6+id.
    charAt(9)*3+id.charAt(10)*7+id.charAt(11)*9+id.charAt(12)*10+id.charAt(13)*5+id.charAt(14)*8+id.charAt(15)*4+id.charAt(16)*2);
    nNum=nNum%11;
    switch (nNum) {
        case 0 :
            result="1";
            break;
        case 1 :
            result="0";
            break;
        case 2 :
            result="X";
            break;
        case 3 :
            result="9";
            break;
        case 4 :
            result="8";
            break;
        case 5 :
            result="7";
            break;
        case 6 :
            result="6";
            break;
        case 7 :
            result="5";
            break;
        case 8 :
            result="4";
            break;
        case 9 :
            result="3";
            break;
        case 10 :
            result="2";
            break;
    }
    //document.write(result);
    return result;
}

/*
 功能:验证身份证号码是否有效
 提 示信息:未输入或输入身份证号不正确!
 使用:validateIdCard(obj,birthday,s)//s:1为男,0为女
 返回:0,1,2,3,4,5
 */
function validateIdCard(obj,birthday,s){
    var aCity={
    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:"国 外"};
    
    var iSum = 0;
    //var info = "";
    var strIDno = obj;
    if(birthday!=null){
        birthday = birthday.replace(/-/g,"/");
    }

    var idCardLength = strIDno.length;
    if(!/^\d{17}(\d|x)$/i.test(strIDno)&&!/^\d{15}$/i.test(strIDno)){
        return 1; //非法身份证号
    }
    if(aCity[parseInt(strIDno.substr(0,2))]==null){
        return 2;// 非法地区
    }

    // 15位身份证转换为18位
    if (idCardLength==15){
        sBirthday = "19" + strIDno.substr(6,2) + "-" + Number(strIDno.substr(8,2)) + "-" + Number(strIDno.substr(10,2));
        var d = new Date(sBirthday.replace(/-/g,"/"));
        var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();
        var genderNo=strIDno.substr(14,1);
        if(sBirthday != dd){
            return 3; //非法生日
        }
        if(birthday==""){
            return 4;//您还没填写出生日期
        }

        if(birthday!=null && d.getTime()!=new Date(birthday).getTime()){
            return 5; //与出生日期不符
        }
        if(s!=null && s!=0 && s!=1){
            return 6;//您还没填写性别
        }

        if(s!=null && genderNo%2!=s){
            return 7;//与性别不符
        }

        strIDno=strIDno.substring(0,6)+"19"+strIDno.substring(6,15);
        strIDno=strIDno+GetVerifyBit(strIDno);
    }
    // 判断是否大于2078年,小于1900年
    var year =strIDno.substring(6,10);
    if (year<1900 || year>2078 ){
        return 3;//非法生日
    }
    /*if(){

     }*/
    //18位身份证处理
    //在后面的运算中x相当于数字10,所以转换成a
    strIDno = strIDno.replace(/x$/i,"a");

    sBirthday=strIDno.substr(6,4)+"-"+Number(strIDno.substr(10,2))+"-"+Number(strIDno.substr(12,2));
    var d = new Date(sBirthday.replace(/-/g,"/"));
    var genderNo=strIDno.substr(16,1);
    if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
        return 3; //非法生日
    }
    if(birthday==""){
        return 4;//您还没填写出生日期
    }
    if(birthday!=null && d.getTime()!=new Date(birthday).getTime()){
        return 5; //与出生日期不符
    }

    if(s!=null && s!=0 && s!=1){
        return 6;//您还没填写性别
    }
    if(s!=null && genderNo%2!=s){
        return 7;//与性别不符
    }
    // 身份证编码规范验证
    for(var i = 17;i>=0;i --){
        iSum += (Math.pow(2,i) % 11) * parseInt(strIDno.charAt(17 - i),11);
    }
    if(iSum%11!=1){
        return 1;// 非法身份证号
    }
    // 判断是否屏蔽身份证
    var words = new Array();
    words = new Array("11111119111111111","12121219121212121");

    for(var k=0;k<words.length;k++){
        if (strIDno.indexOf(words[k])!=-1){
            return 1;
        }
    }
    return 0;
}

//身份证(无关联验证)
$.validator.addMethod("cretID", function(value, element, param){
    var n=validateIdCard(value);
    var error=["","非法身份证号","地区编号不合法","出生日期不合法"];
    param[1]=error[n];
    return this.optional(element) || n==0;

},$.validator.format("{1}"));
Copy after login


The above is the content of the js commonly used regular expressions, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

How to verify password using regular expression in Go? How to verify password using regular expression in Go? Jun 02, 2024 pm 07:31 PM

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

Chinese character filtering: PHP regular expression practice Chinese character filtering: PHP regular expression practice Mar 24, 2024 pm 04:48 PM

PHP is a widely used programming language, especially popular in the field of web development. In the process of web development, we often encounter the need to filter and verify text input by users, among which character filtering is a very important operation. This article will introduce how to use regular expressions in PHP to implement Chinese character filtering, and give specific code examples. First of all, we need to clarify that the Unicode range of Chinese characters is from u4e00 to u9fa5, that is, all Chinese characters are in this range.

See all articles