「JS」常用正则

检验手机号

    checkPhone: function(phone) {
        // var reg = /^1[3|4|5|7|8][0-9]{9}$/; 
        //验证规则,第一位是【1】开头,第二位有【3,4,5,7,8】,第三位及以后可以是【0-9】  
        var reg = /^1[0-9]{10}$/; //不验证第二位,防止几年后新增号码段  
        if (!reg.test(phone)) {
            return false;
        }
        return true;
    }

检验身份证

    /*
     * 检验身份证
     */
    checkIdCard: function(idCard) {
        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 = "";
        if (!/^\d{17}(\d|x)$/i.test(idCard)) {
            return false; //身份证长度或格式错误  
        }
        idCard = idCard.replace(/x$/i, "a");
        if (aCity[parseInt(idCard.substr(0, 2))] == null) {
            return false; //身份证地区非法;  
        }
        var sBirthday = idCard.substr(6, 4) + "-" + Number(idCard.substr(10, 2)) + "-" + Number(idCard.substr(12, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"));
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
            return false; //身份证上的出生日期非法;  
        }
        for (var i = 17; i >= 0; i--) {
            iSum += (Math.pow(2, i) % 11) * parseInt(idCard.charAt(17 - i), 11);
        }
        if (iSum % 11 != 1) {
            return false; //身份证号非法;   
        }

        return true;

    },

检验否是汉字

    /* 
     * 检验否是汉字  
     */
    checkCharacter: function(charValue) {
        var reg = /^[\u4e00-\u9fa5]{0,}$/;
        if (!reg.test(charValue)) {
            return false;
        }
        return true;
    }

获取浏览器参数

function GetQueryStringRegExp(name,url) {
var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i"); 
if (reg.test(url)) 
    return decodeURIComponent(RegExp.$2.replace(/\+/g, " ")); 
}

移动设备检测

var BrowserInfo = {    
    userAgent: navigator.userAgent.toLowerCase()
    isAndroid: Boolean(navigator.userAgent.match(/android/ig)),
    isIphone: Boolean(navigator.userAgent.match(/iphone|ipod/ig)),
    isIpad: Boolean(navigator.userAgent.match(/ipad/ig)),
    isWeixin: Boolean(navigator.userAgent.match(/MicroMessenger/ig)),
}
/**
 * 判断是否移动设备
 */
function isMobile() {
    if (typeof this._isMobile === 'boolean') {
        return this._isMobile;
    }
    var screenWidth = this.getScreenWidth();
    var fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport
            || rendererModel.runningExperiments.fixviewport;
    var fixViewPortsExperimentRunning = fixViewPortsExperiment
            && (fixViewPortsExperiment.toLowerCase() === "new");
    if (!fixViewPortsExperiment) {
        if (!this.isAppleMobileDevice()) {
            screenWidth = screenWidth / window.devicePixelRatio;
        }
    }
    var isMobileScreenSize = screenWidth < 600;
    var isMobileUserAgent = false;
    this._isMobile = isMobileScreenSize && this.isTouchScreen();
    return this._isMobile;
}
/**
 * 判断是否移动设备访问
 */
function isMobileUserAgent() {
    return (/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i
            .test(window.navigator.userAgent.toLowerCase()));
}
/**
 * 判断是否苹果移动设备访问
 */
function isAppleMobileDevice() {
    return (/iphone|ipod|ipad|Macintosh/i.test(navigator.userAgent
            .toLowerCase()));
}
/**
 * 判断是否安卓移动设备访问
 */
function isAndroidMobileDevice() {
    return (/android/i.test(navigator.userAgent.toLowerCase()));
}
/**
 * 判断是否Touch屏幕
 */
function isTouchScreen() {
    return (('ontouchstart' in window) || window.DocumentTouch
            && document instanceof DocumentTouch);
}
/**
 * 判断是否在安卓上的谷歌浏览器
 */
function isNewChromeOnAndroid() {
    if (this.isAndroidMobileDevice()) {
        var userAgent = navigator.userAgent.toLowerCase();
        if ((/chrome/i.test(userAgent))) {
            var parts = userAgent.split('chrome/');

            var fullVersionString = parts[1].split(" ")[0];
            var versionString = fullVersionString.split('.')[0];
            var version = parseInt(versionString);

            if (version >= 27) {
                return true;
            }
        }
    }
    return false;
}

生成随机字符串

function generateRandomAlphaNum(len) {
    var rdmString = "";
    for( ; rdmString.length < len; rdmString  += Math.random().toString(36).substr(2));
    return  rdmString.substr(0, len);
}
//调用
generateRandomAlphaNum(10);//fa8y393e9a
generateRandomAlphaNum(10);//j15mhox8bs
Posted in JS