常用JS验证类库

1.common.js

 

// JavaScript Document
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖保佑             永无BUG
/** 【原创】常用JS验证类库 **/
var Common = {
    /**
     * 检查输入字符串是否为空或者全部都是空格
     * @param  str 需要验证的字符串
     * @return bool
     */
    'isNull': function(str) {
        if (str == "") return true;
        var reg_str = "^[ ]+$";
        return reg_str.test(str);
    },

    /**
     * 验证是否为合法的手机号
     * @param mobile  手机号
     * @param reg_exp [非必传]验证规则【因为电话号码的规则有可能改,所以这个是一个参数】
     * @return bool
     */
    'isMobile': function(mobile) {
        var reg_exp = arguments[1] ? arguments[1] : /^(((1[345789]{1}))+\d{9})$/;
        if (mobile.length == 0) {
            return false;
        }
        if (mobile.length != 11) {
            return false;
        }
        return reg_exp.test(mobile);
    },

    /**
     * 验证是否是合法的邮箱
     * @param email   邮箱地址
     * @param reg_exp [非必传]验证规则【如果有特殊需求,可以自定义规则】
     * @return bool
     */
    'isEmail': function(email) {
        var reg_exp = arguments[1] ? arguments[1] : /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        if (email.length == 0) {
            return false;
        }
        return reg_exp.test(email);
    },

    /**
     * 验证是否是合法的固定电话
     * @param phone 固定电话
     * @return bool
     */
    'isPhone': function(phone) {
        var reg_exp = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
        if (phone == "") {
            return false;
        }
        return reg_exp.test(phone);
    },

    /**
     * 验证是否是邮编
     * @param postcode 邮编
     * @return bool
     */
    'isPostcode': function(postcode) {
        var reg_exp = /^\d{6}$/;
        if (postcode == "") {
            return false;
        }
        return reg_exp.test(postcode);
    },

    /**
     * 验证是由0-9组成的数字不能有小数点
     * @param number 数字
     * @return bool
     */
    'isNumber': function(number) {
        var reg_exp = /^[0-9]+$/;
        if (number == "") {
            return false;
        }
        return reg_exp.test(number);
    },

    /**
     * 验证是由0-9组成的数字可以能有小数点并且保留2位
     * @param double_number 数字
     * @return bool
     */
    'isDoubleNumber': function(double_number) {
        var reg_exp = /^[0-9]+(\.\d{2})?$/;
        if (double_number == "") {
            return false;
        }
        return reg_exp.test(double_number);
    },

    /**
     * 验证是否是中文
     * @param str 字符串
     * @return bool
     */
    'isChinese': function(str) {
        var reg_exp = /^[\u0391-\uFFE5]+$/;
        if (str == "") {
            return false;
        }
        return reg_exp.test(str);
    },

    /**
     * 根据参数名获取参数值
     * @param str 参数名字符串
     * @return str 参数值字符串
     */
    'getQueryStringByName': function(name) {
        var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
        if (result == null || result.length < 1) {
            return "";
        }
        return result[1];
    },

    /**
     * 文件扩展名匹配正则
     * @param file.name
     * @return str 字符串
     */
    'getFileExtendingName': function(filename) {
        // 文件扩展名匹配正则
        var reg = /\.[^\.]+$/;
        var matches = reg.exec(filename);
        if (matches) {
            return matches[0];
        }
        return '';
    },

    /**
     * 生成3-32位随机串:randomWord(true, 3, 32)
     * 生成43位随机串:randomWord(false, 43)
     * randomWord 产生任意长度随机字母数字组合
     * randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
     */
    'randomWord': function(randomFlag, min, max) {
        var str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                'I',
                'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            ];

        if (randomFlag) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (var i = 0; i < range; i++) {
            pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    },

    /**
     * 获取随机UUID
     * @return str 字符串
     */
    'genUid': function() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0,
                v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },

    /**
     * 生成6位随机ID
     * @return str 字符串
     */
    'genID': function() {
        return Math.floor(Math.random() * (999999 - 100000)) + 100000;
    },

    /**
     * 生成8位年月日字符串
     * @return str 字符串
     */
    'genData': function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        year = "" + year;
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        return year + month + day;
    },
};

 

2.common.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>Common</title>
    </head>
    <body>
        <script src="common.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" charset="utf-8">
            var test = String(parseInt(Math.random() * 1000000, 10));
            console.log(test);
            // 生成16位随机串
            console.log(Common.randomWord(false, 16));
            
            // 生成6位随机ID
            console.log(Common.genID());
            
            // 生成8位年月日字符串
            console.log(Common.genData());
        </script>
    </body>
</html>

 

  

posted @ 2020-12-04 00:57  杵臼  阅读(395)  评论(0编辑  收藏  举报