码迷,mamicode.com
首页 > Web开发 > 详细

JS--easyui通用验证

时间:2019-06-28 18:05:21      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:async   开头   var   sock   als   汉字   case   密码   idcard   

// JavaScript Document 


$.extend($.fn.validatebox.defaults.rules, {
    Composite_validation:
    {
        validator: function (value, param) {
            var mReg = new RegExp(param[0]); //传递过来的正则字符串中的"\"必须是"\\"  
            if (!mReg.test(value)) {
                $.fn.validatebox.defaults.rules.Composite_validation.message = param[1];
                return false;
            }
            else {
                var postdata = {};
                postdata[param[3]] = value;
                var result = $.ajax({
                    url: param[2],
                    data: postdata,
                    async: false,
                    type: "post",
                    dataType: "json"
                }).responseText;
                result = JSON.parse(result);
                if (!result.success) {
                    $.fn.validatebox.defaults.rules.Composite_validation.message = result.message;
                    return false;
                }
                else {
                    return true;
                }
            }
        },
        message: ‘‘
    },

    sockdate: {
        validator: function (value, param) {
            //var s = $("input[name="+param[0]+"]").val();
            //因为日期是统一格式的所以可以直接比较字符串 否则需要Date.parse(_date)转换
            value = value.replace(/-/g, "/");//替换字符,变成标准格式 
            var d1 = new Date(Date.parse(value));
            var d2 = new Date();
            return d1.format("yyyyMMdd") >= d2.format("yyyyMMdd");
        },
        message: ‘到货日期必须大于或等于当前日期‘
    },
    URL: {
        validator: function (value, param) {
            return /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/.test(value);
        },
        message: ‘请输入URL‘
    },
    CHS: {
        validator: function (value, param) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message: ‘请输入汉字‘
    },
    ZIP: {
        validator: function (value, param) {
            return /^[1-9]\d{5}$/.test(value);
        },
        message: ‘邮政编码不存在‘
    },
    QQ: {
        validator: function (value, param) {
            return /^[1-9]\d{4,10}$/.test(value);
        },
        message: ‘QQ号码不正确‘
    },
    mobile: {
        validator: function (value, param) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/.test(value);
        },
        message: ‘手机号码不正确!示例:13012341234‘
    },
    tel: { // 电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号)
        validator: function (value, param) {
            return /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(value);
        },
        message: ‘电话号码不正确!示例:13012341234、0512-6234567、0512-6234567-0001‘
    },
    text: {
        validator: function (value, param) {
            return /^[\u0391-\uFFE5\w]+$/.test(value);
        },
        message: ‘文本内容只允许汉字、英文字母、数字及下划线。‘
    },

    validTag: {
        validator: function (value, param) {
            var valid = true;
            if (value.length != param[0]) {
                valid = false;
                param[1] = ‘输入内容长度必须为:‘ + param[0];
            } else if (!(/^[A-F0-9]{6}$/.test(value))) {
                valid = false;
                param[1] = ‘只允许输入[000000~FFFFFF]之间的标签ID。小贴士:输入标签ID后6位。‘;
            }
            return valid;
        },
        message: ‘{1}‘
    },

    validTagExtend: {
        validator: function (value, param) {
            var valid = true;
            if (value.length > param[0]) {
                valid = false;
                param[1] = ‘标签编号长度不能超过‘ + param[0];
            } else if (!(/^[A-Z0-9]+$/.test(value))) {
                valid = false;
                param[1] = ‘标签编号必须是A-Z(大写)之间的英文字符或数字‘;
            }
            return valid;
        },
        message: ‘{1}‘
    },

    validStation: {
        validator: function (value, param) {
            var valid = true;
            if (value.length != param[0]) {
                valid = false;
                param[1] = ‘输入内容长度必须为:‘ + param[0];
            } else if (!(/^[A-F0-9]{2}$/.test(value))) {
                valid = false;
                param[1] = ‘只允许输入[00~FF]之间的基站ID。‘;
            }
            return valid;
        },
        message: ‘{1}‘
    },

    textLength: {
        validator: function (value, param) {
            var valid = true;
            if (value.length < param[0] || value.length > param[1]) {
                valid = false;
                param[2] = ‘输入内容长度必须介于‘ + param[0] + ‘和‘ + param[1] + ‘之间‘;
            } else if (!(/^[\u0391-\uFFE5\w\-\.]+$/.test(value))) {
                valid = false;
                param[2] = ‘文本内容只允许汉字、英文字母、数字、小数点、中线及下划线。‘;
            }
            return valid;
        },
        message: ‘{2}‘
    },
    safepass: {
        validator: function (value, param) {
            var valid = true;
            if (value.length > param[0]) {
                valid = false;
                param[1] = ‘至多输入 ‘ + param[0] + ‘ 字符‘;
            } else if (!safePassword(value)) {
                valid = false;
                param[1] = ‘密码由字母、特殊字符和数字组成,至少6位‘;
            }
            return valid;
        },
        message: ‘{1}‘
    },
    equalTo: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: ‘两次输入的字符不一至‘
    },
    number: {
        validator: function (value, param) {
            return /^\d+$/.test(value);
        },
        message: ‘请输入数字‘
    },
    number2: {
        validator: function (value, param) {
            return /^\+?[0-9]\d*$/.test(value);
        },
        message: ‘请输入大于零的数字‘
    },
    idcard: {
        validator: function (value, param) {
            return idCard(value);
        },
        message: ‘请输入正确的身份证号码‘
    },
    minLength: {
        validator: function (value, param) {
            return value.length >= param[0];
        },
        message: ‘至少输入 {0} 字符‘
    },
    maxLength: {
        validator: function (value, param) {
            return value.length <= param[0];
        },
        message: ‘至多输入 {0} 字符‘
    },
    length: {
        validator: function (value, param) {
            var len = $.trim(value).length;
            return len >= param[0] && len <= param[1];
        },
        message: "输入内容长度必须介于{0}和{1}之间."
    },
    selectRequired: {
        validator: function (value, param) {
            var selectVal = $("input[name=" + param[0] + "]").val(); // $(param[0]).val();
            return parseInt(selectVal) > -1;
        },
        message: ‘请选择‘
    },
    areaCode: {
        validator: function (value, param) {
            var valid = true;
            if (!(/^[A][0-9]{4}$/.test(value))) {
                valid = false;
            }
            return valid;
        },
        message: ‘区域编号必须以A(大写)开头+4位数字,例如A0001‘
    },
    shelfCode: {
        validator: function (value, param) {
            var valid = true;
            if (!(/^[S][0-9]{5}$/.test(value))) {
                valid = false;
            }
            return valid;
        },
        message: ‘货架编号必须以S(大写)开头+5位数字,例如S00001‘
    }
});

/* 密码由字母和数字组成,至少6位 */
var safePassword = function (value) {
    return !(/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\‘\"]*)|.{0,5})$|\s/.test(value));
};

var idCard = function (value) {
    if (value.length == 18 && 18 != value.length) return false;
    var number = value.toLowerCase();
    var d, sum = 0, v = ‘10x98765432‘, w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = ‘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 re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
    if (re == null || a.indexOf(re[1]) < 0) return false;
    if (re[2].length == 9) {
        number = number.substr(0, 6) + ‘19‘ + number.substr(6);
        d = [‘19‘ + re[4], re[5], re[6]].join(‘-‘);
    } else d = [re[9], re[10], re[11]].join(‘-‘);
    if (!isDateTime.call(d, ‘yyyy-MM-dd‘)) return false;
    for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i];
    return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
}

var isDateTime = function (format, reObj) {
    format = format || ‘yyyy-MM-dd‘;
    var input = this, o = {}, d = new Date();
    var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
    var len = f1.length, len1 = f3.length;
    if (len != f2.length || len1 != f4.length) return false;
    for (var i = 0; i < len1; i++) if (f3[i] != f4[i]) return false;
    for (var i = 0; i < len; i++) o[f1[i]] = f2[i];
    o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
    o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
    o.dd = s(o.dd, o.d, d.getDate(), 31);
    o.hh = s(o.hh, o.h, d.getHours(), 24);
    o.mm = s(o.mm, o.m, d.getMinutes());
    o.ss = s(o.ss, o.s, d.getSeconds());
    o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
    if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false;
    if (o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
    d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
    var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
    return reVal && reObj ? d : reVal;

    function s(s1, s2, s3, s4, s5) {
        s4 = s4 || 60, s5 = s5 || 2;
        var reVal = s3;
        if (s1 != undefined && s1 != ‘‘ || !isNaN(s1)) reVal = s1 * 1;
        if (s2 != undefined && s2 != ‘‘ && !isNaN(s2)) reVal = s2 * 1;
        return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
    }
};

 

JS--easyui通用验证

标签:async   开头   var   sock   als   汉字   case   密码   idcard   

原文地址:https://www.cnblogs.com/zhaoyl9/p/11104057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!