美文网首页
正则校验

正则校验

作者: 一蓑烟雨任平生_cui | 来源:发表于2017-12-28 13:40 被阅读0次
    1. 手机号
    let tel = /^1(3[0-9]|4[57]|5[0-35-9]|7[013678]|8[0-9])\d{8}$/;
    
    2. 邮箱
    let email = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*(\.[a-zA-Z]+)$/;
    
    3. 身份证
    let IDCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    
    4. URL (支持http/https/ftp协议)
    let url = /^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i;
    
    5. 汉字
    let chinese = /^[\u4e00-\u9fa5]{0,3}$/;  // 0-3个汉字
    
    6. 过滤xss
    let outXss = /(<script.*?>.*?(<\/script>)?)|(.*?<\/script>)/i;
    
    7. n位小数
    let floatN = /^\d(\.\d{n})$/;
    
    8. 最多n为小数
    let floatMostN = /^\d(\.\d{1, n})?$/; 
    
    9. 汉字,数字,字母
    let result = /^[\u4e00-\u9fa5a-zA-Z0-9]$/
    
    身份证精确验证
    const isIdCard = sId => {
        let iSum = 0;
        const 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: "国外"
        };
    
        if (!/^\d{17}(\d|x)$/i.test(sId)) {
            return false;
        }
    
        sId = sId.replace(/x$/i, 'a');
    
        if (aCity[parseInt(sId.substr(0, 2), 10)] == null) {
            return false;
        }
    
        let sBirthday = [sId.substr(6, 4), Number(sId.substr(10, 2)), Number(sId.substr(12, 2))].join("/");
    
        let d = new Date(sBirthday);
    
        if (sBirthday != [d.getFullYear(), d.getMonth() + 1, d.getDate()].join("/")) {
            return false;
        }
    
        for (let i = 17; i >= 0; i--) {
            iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
        }
    
        return iSum % 11 == 1;
    };
    

    获取URL参数值

    getUrlKey = name => {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
    }
    

    相关文章

      网友评论

          本文标题:正则校验

          本文链接:https://www.haomeiwen.com/subject/nyzmgxtx.html