Easyui 驗證驗證擴展,限制combobox 只能輸入選項內容


$.extend($.fn.validatebox.defaults.rules, {
    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 /^0?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/.test(value);
        },
        message: '手機號碼不正確'
    },
    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'
    },
    loginName: {
        validator: function (value, param) {
            return /^[\u0391-\uFFE5\w]+$/.test(value);
        },
        message: '登錄名稱只允許漢字、英文字母、數字及下划線。'
    },
    safepass: {
        validator: function (value, param) {
            return safePassword(value);
        },
        message: '密碼由字母和數字組成,至少6位'
    },
    /*
    <input type="password" id="txtpasswd" />
    <input type="password" id="txtpasswd2" validType="equalTo['#txtpasswd']" />
    */
    equalTo: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: '兩次輸入的字符不一至'
    },
    number: {
        validator: function (value, param) {
            return /^\d+$/.test(value);
        },
        message: '請輸入數字'
    },
    money: {
        validator: function (value, param) {
            return /^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/.test(value);
        },
        message: '請輸入金額(例:8.88或8)'
    },
    ip: {// 驗證IP地址 
        validator: function (value) {
            return /d+.d+.d+.d+/i.test(value);
        },
        message: 'IP地址格式不正確'
    },
    idcard: {
        validator: function (value, param) {
            return idCard(value);
        },
        message: '請輸入正確的身份證號碼'
    },
    title: {
        validator: function (value) {
            return /^[\u0391-\uFFE5\w]+$/.test(value);
        },
        message: '只允許漢字、英文字母、數字及下划線。'
    },
    messageFormat: {
        validator: function (value) {
            return value.indexOf(" ") < 0;
        },
        message: '不能輸入特殊字符!'
    },
    dateCompare: {
        validator: function (value, param) {
            //var d1 = $.fn.datebox.defaults.parser(param[0]);
            var d1 = $(param[0]).datetimebox('getValue');
            var d2 = value;
            return d2 >= d1;
        },
        message: '{1}'
    },
    dateCompareLess: {
        validator: function (value, param) {
            //var d1 = $.fn.datebox.defaults.parser(param[0]);
            var d1 = $(param[0]).datetimebox('getValue');
            var d2 = value;
            return d2 <= d1;
        },
        message: '{1}'
    },
    //data-options="validType:['email','isExists[\'EmailIsExists\',\'fdfdfd\']']" 多個調用 
    //data-options="validType:['isExists[\'EmailIsExists\',\'fdfdfd\']']" 多個調用 
    isExists: {
        validator: function (value, param) {
            if ($.trim(value) == "") {
                return true;
            }

            var result = isExists(param[0], param[2], param[3], value);
            return result;
            //if (param) {
            //this.message.replace("{0}", param[1]+"||"+param[0]);
            //for (var i = 0; i < param.length; i++) {
            //    this.message = this.message.replace(new RegExp(
            //        "\\{" + i + "\\}", "g"), param[i]);
            //}
            //}
        },
        message: '{1}' //{0就是第0個參數,1則為第一個參數}
    },
    comboBoxRequired: {
        validator: function (value, param) {
            var isRequired = $("#" + param[0]).attr(param[1]);
            if (isRequired != 1 && (value == "" || value.indexOf('選擇') >= 0 || value.indexOf('全部') >= 0)) {
                return false;
            } else {
                return true;
            }
        },
        message: '該下拉框為必選項'
    },
    comboBoxEditvalid: {
        validator: function (value, param) {
            var $combobox = $("#" + param[0]);
            if (value) {
                if ($combobox.combobox('getValue') == $combobox.combobox('getText'))
                    return false;
                return true;
            }
            return false;
            
        },
        message: '請選擇下拉框選項,不要直接使用輸入內容'
    },
    //用戶賬號驗證(只能包括 _ 數字 字母) 
    account: {//param的值為[]中值
        validator: function (value, param) {
            if (value.length < param[0] || value.length > param[1]) {
                $.fn.validatebox.defaults.rules.account.message = '用戶名長度必須在' + param[0] + '' + param[1] + '范圍';
                return false;
            } else {
                if (!/^[\w]+$/.test(value)) {
                    $.fn.validatebox.defaults.rules.account.message = '用戶名只能數字、字母、下划線組成.';
                    return false;
                } else {
                    return true;
                }
            }
        }, message: ''
    }
});
 <input name="CatelogParentId" id="CatelogParentId"  class="easyui-combobox" type="text" data-options="required:true,validType:'comboBoxEditvalid[\'CatelogParentId\']'" >
Easyui.initCombobox = function (options) {
    var $combobox = $("#" + options.id);
    var _options = $.extend({
        valueField: 'id',
        textField: 'text',
        validType: 'comboBoxEditvalid[\'' + options.id + '\',\'isRequired\']',
        url: encodeURIComponent(options.url)
    }, options);
    $combobox.combobox(_options);
};
@model Rednoble.OMS.CMS.Infastracture.Until.CustomCombobox
<input id="@Model.Id" name="@Model.Name" class="easyui-combobox" data-options="required:true,validType:['comboBoxRequired[\'@(Model.Id)\',\'isRequired\']','comboBoxEditvalid[\'@(Model.Id)\']'],editable:@Model.Editable.ToString().ToLower()" />
<script type="text/javascript">
    $(document).ready(function () {
        Easyui.initCombobox({
            url: "@(Model.Url+Model.SpitStr)Text=" + escape("@Model.Text"),
            id: "@Model.Id",
            onLoadSuccess: function() {
                if ("@Model.IsRequest" == "false" || "@Model.IsRequest" == "False") {
                    //取消必填
                    $("#" + "@Model.Id").attr("isRequired", "1").parent().find(".validatebox-invalid").removeClass("validatebox-invalid");
                }
                if ('@Model.Value') {
                    $("#" + "@Model.Id").combobox('setValue', '@Model.Value');
                }
            }
        });
    });

    function toBool(str) {
        if (str == 'True' || str == 'true' || str == '1')
            return true;
        return false;
    }
</script>

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM