Home > Web Front-end > JS Tutorial > body text

A collection of commonly used regular expressions in js

黄舟
Release: 2017-02-22 13:28:27
Original
1559 people have browsed it

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!


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template