当前位置:首页 >> 网络编程

easyui关于validatebox实现多重规则验证的方法(必看)

方法一:

自从1.3.2版本开始,validatebox自身已经支持多重校验了,例如:

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

方法二:(不太好用,试了半天还是不显示第二个验证的消息)

对于1.5版本的easyui.min,注释掉以下代码:

easyui关于validatebox实现多重规则验证的方法(必看)

然后再添加

$.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}.' 
      } 
    });

调用方法

<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])"输入内容长度必须介于{0}和{1}之间."
      },
 phone: {// 验证电话号码
        validator: function (value) {
          return /^((\d{2,3})|(\d{3}\-))"#" + param[0]).val() != "" && value != "") {
            return $("#" + param[0]).val() == value;
          } else {
            return true;
          }
        },
        message: '两次输入的密码不一致!'
      }

以上这篇easyui关于validatebox实现多重规则验证的方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。