方法一:
自從1.3.2版本開始,validatebox自身已經支持多重校驗了,例如:
<input class="easyui-validatebox" data-options="required:true,validType:['email','length[0,20]']">
方法二:(不太好用,試了半天還是不顯示第二個驗證的消息)
對于1.5版本的easyui.min,注釋掉以下代碼:

然后再添加
$.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( "http://{" + i + "http://}", "g"), param[i]); } } return len >= param[0] && len <= param[1]; }, message : 'Please enter a value between {0} and {1}.' } });調用方法
<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兩邊不能寫引號)
<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: '兩次輸入的密碼不一致!' }以上這篇easyui關于validatebox實現多重規則驗證的方法(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答