jquery.easyui.extension.js 7.35 KB
/// <reference path="jquery.min.js" />
/// <reference path="jquery.easyui.min.js" />

/*
  说明:扩展EasyUI的验证规则
*/
$.extend($.fn.validatebox.defaults.rules, {
    //验证身份证
    idcard: {
        validator: function(value) {
            return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(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}个字符'
    },
    phone: { // 验证电话号码 
        validator: function(value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '格式不正确,请使用下面格式:020-88888888'
    },
    mobile: { // 验证手机号码 
        validator: function(value) {
            return /^(13|15|18)\d{9}$/i.test(value);
        },
        message: '手机号码格式不正确'
    },
    currency: { // 验证货币 
        validator: function(value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message: '货币格式不正确'
    },
    number: { // 验证整数或小数 
        validator: function(value) {
            return /[-+]?\b[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?\b/.test(value);
        },
        message: '请输入数字,并确保格式正确'
    },
    integer: { // 验证整数 
        validator: function(value) {
            return /^[+]?[1-9]+\d*$/i.test(value);
        },
        message: '请输入整数'
    },
    age: { // 验证年龄
        validator: function(value) {
            return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);
        },
        message: '年龄必须是0到120之间的整数'
    },
    chinese: { // 验证中文 
        validator: function(value) {
            return /^[\Α-\¥]+$/i.test(value);
        },
        message: '请输入中文'
    },
    english: { // 验证英语 
        validator: function(value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message: '请输入英文'
    },
    username: { // 验证用户名 
        validator: function(value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message: '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
    },
    faxno: { // 验证传真 
        validator: function(value) {
            //            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value); 
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '传真号码不正确'
    },
    zip: { // 验证邮政编码 
        validator: function(value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: '邮政编码格式不正确'
    },
    ip: { // 验证IP地址 
        validator: function(value) {
            return /^\d+.\d+.\d+.\d+(:\d+)?$/i.test(value);
        },
        message: 'IP地址格式不正确'
    },
    name: { // 验证姓名,可以是中文或英文 
        validator: function(value) {
            return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
        },
        message: '请输入姓名'
    },
    date: { // 验证姓名,可以是中文或英文 
        validator: function(value) {
            //格式yyyy-MM-dd或yyyy-M-d
            return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
        },
        message: '日期格式不正确'
    },
    equals: {
        validator: function(value, param) {
            if ($("#" + param[0]).val() != "" && value != "") {
                return $("#" + param[0]).val() == value;
            } else {
                return true;
            }
        },
        message: '两次输入不匹配'
    },
    regex: {
        validator: function(value, param) {
            var regex = new RegExp(param[0]);
            return regex.test(value);
        },
        message: "格式不合法"
    },
    longitude: {
        validator: function(value, param) {
            var regex = /^[\-\+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$/;
            return regex.test(value);
        },
        message: "经度格式不合法"
    },
    latitude: {
        validator: function(value, param) {
            var regex = /^[\-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/;
            return regex.test(value);
        },
        message: "纬度格式不合法"
    },
    //不能为空,参数为不能取的值的数组 ["","-1","全部"]
    notEmpty: {
        validator: function(value, param) {
            var isValidate = true;
            for (var i = 0; i < param.length; i++) {
                if (value == param[i]) {
                    return false;
                }
            }
            return true;
        },
        message: "不能为空"
    },
    range: {
        validator: function(value, param) {
            return value >= param[0] && value <= param[1];
        },
        message: "值必须在{0}与{1}之间"
    }
});

//扩展Tab组件,添加遮罩
(function() {
    $.extend($.fn.tabs.methods, {
        //显示遮罩
        loading: function(jq, msg) {
            return jq.each(function() {
                var panel = $(this).tabs("getSelected");
                if (msg == undefined) {
                    msg = "正在加载数据,请稍候...";
                }
                $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: panel.width(), height: panel.height() + 30 }).appendTo(panel);
                $("<div class=\"datagrid-mask-msg\"></div>").html(msg).appendTo(panel).css({ display: "block", left: (panel.width() - $("div.datagrid-mask-msg", panel).outerWidth()) / 2, top: (panel.height() - $("div.datagrid-mask-msg", panel).outerHeight()) / 2 });
            });
        },
        //隐藏遮罩
        loaded: function(jq) {
            return jq.each(function() {
                var panel = $(this).tabs("getSelected");
                panel.find("div.datagrid-mask-msg").remove();
                panel.find("div.datagrid-mask").remove();
            });
        }
    });
})(jQuery);

//扩展datagrid editor
$.extend($.fn.datagrid.defaults.editors, {
    my97: {
        init: function(container, options) {
            var input = $('<input class="Wdate" onfocus="WdatePicker({dateFmt:\'' + options.dateFmt + '\',minDate:\'' + options.minDate + '\',maxDate:\'' + options.maxDate + '\',readOnly:true});"/>').appendTo(container);
            return input;
        },
        getValue: function(target) {
            return $(target).val();
        },
        setValue: function(target, value) {
            $(target).val(value);
        },
        resize: function(target, width) {
            var input = $(target);
            if ($.boxModel == true) {
                input.width(width - (input.outerWidth() - input.width()));
            } else {
                input.width(width);
            }
        }
    }
});