easyui關於validatebox實現多重規則驗證的實踐

參考資料javascript

http://blog.csdn.net/jumtre/article/details/38510975html

http://blog.csdn.net/lybwwp/article/details/9028741/java

方法一:ui

自從1.3.2版本開始,validatebox自身已經支持多重校驗了,例如:this

<input class="easyui-validatebox" data-options="required:true,validType:['email','length[0,20]']">  

 方法二:(不太好用,試了半天仍是不顯示第二個驗證的消息)編碼

對於1.5版本的easyui.min,註釋掉如下代碼:spa

 
而後再添加
$.extend($.fn.validatebox.defaults.rules, {  
            multiple : {  
                validator : function(value, vtypes) {  
                    var returnFlag = true;  
                    var opts = $.fn.validatebox.defaults;  
                    for (var i = 0; i < vtypes.length; i++) {  
                        var methodinfo = /([a-zA-Z_]+)(.*)/.exec(vtypes[i]);  
                        var rule = opts.rules[methodinfo[1]];  
                        if (value && rule) {  
                            var parame = eval(methodinfo[2]);  
                            if (!rule["validator"](value, parame)) {  
                                returnFlag = false;  
                                this.message = rule.message;  
                                break;  
                            }  
                        }  
                    }  
                    return returnFlag;  
                }  
            },  
            length : {  
                validator : function(value, param) {  
                    this.message = 'Please enter a value between {0} and {1}.';  
                    var len = $.trim(value).length;  
                    if (param) {  
                        for (var i = 0; i < param.length; i++) {  
                            this.message = this.message.replace(new RegExp(  
                                            "\\{" + i + "\\}", "g"), param[i]);  
                        }  
                    }  
                    return len >= param[0] && len <= param[1];  
                },  
                message : 'Please enter a value between {0} and {1}.'  
            }  
        });  

調用方法.net

<input class="easyui-validatebox" data-options="required:true,validType:'multiple[\'email\',\'length[0,20]\']'">  

 

方法三:(能夠實現兩種驗證的消息)

$.extend($.fn.validatebox.defaults.rules, {  
    minLength : {  
        validator : function (value, param) {  
            var rules = $.fn.validatebox.defaults.rules;  
            rules.minLength.message = 'Please enter at least {0} characters.';  
            if(!rules.email.validator(value)){  
                rules.minLength.message = rules.email.message;  
                return false;  
            }  
            if(!rules.length.validator(value,param)){  
                rules.minLength.message = rules.length.message;  
                return false;  
            }  
            return value.length >= param[0];  
        },  
        message : ''  
    }  
});  

根據方法三的試驗:日誌

 

$.extend($.fn.validatebox.defaults.rules, {
   
    //再次輸入密碼效驗(與上一次同樣;密碼介於6-16位)
    checkpwd: {
        validator: function (value, param) {
            var rules = $.fn.validatebox.defaults.rules;
            rules.checkpwd.message = 'Please enter at least {0} characters.';
            
            if (!rules.passequals.validator(value,param)) {
                rules.checkpwd.message = rules.passequals.message;
                return false;
            }
            if (!rules.minlength.validator(value)) {
                rules.checkpwd.message = rules.minlength.message;
                return false;
            }
            return value.length >= param[0];
        },
        message: ''
    },
    passequals: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: '兩次密碼不一致.'
    },
   
    minlength: {
        validator: function (value) {
            var len = $.trim(value).length;
            return len >=6 && len <= 16;
        },
        message: "輸入內容長度必須介於6和16之間."
    }
});

調用:(注意pwd兩邊不能寫引號)code

<input id="pwd" name="pwd" type="password" class="easyui-validatebox"  />
<input id="rpwd" name="rpwd" type="password" class="easyui-validatebox" data-options="validType:'checkpwd[pwd]'" />

 

附錄: 能夠參考的驗證規則:

 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: '請輸入至少(2)個字符.'
            },
 length: { validator: function (value, param) {
                var len = $.trim(value).length;
                return len >= param[0] && len <= param[1];
            },
                message: "輸入內容長度必須介於{0}和{1}之間."
            },
 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: '手機號碼格式不正確'
            },
intOrFloat: {// 驗證整數或小數
                validator: function (value) {
                    return /^\d+(\.\d+)?$/i.test(value);
                },
                message: '請輸入數字,並確保格式正確'
            },
currency: {// 驗證貨幣
                validator: function (value) {
                    return /^\d+(\.\d+)?$/i.test(value);
                },
                message: '貨幣格式不正確'
            },
qq: {// 驗證QQ,從10000開始
                validator: function (value) {
                    return /^[1-9]\d{4,9}$/i.test(value);
                },
                message: 'QQ號碼格式不正確'
            },
integer: {// 驗證整數 可正負數
                validator: function (value) {
                    //return /^[+]?[1-9]+\d*$/i.test(value);

                    return /^([+]?[0-9])|([-]?[0-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: '請輸入英文'
            },
unnormal: {// 驗證是否包含空格和非法字符
                validator: function (value) {
                    return /.+/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+/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: '清輸入合適的日期格式'
            },
msn: {
                validator: function (value) {
                    return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
                },
                message: '請輸入有效的msn帳號(例:abc@hotnail(msn/live).com)'
            },
same: {
                validator: function (value, param) {
                    if ($("#" + param[0]).val() != "" && value != "") {
                        return $("#" + param[0]).val() == value;
                    } else {
                        return true;
                    }
                },
                message: '兩次輸入的密碼不一致!'
            }
      
相關文章
相關標籤/搜索