【问题标题】:How to switch digits validator by function in jQuery validation plugin?如何在 jQuery 验证插件中按功能切换数字验证器?
【发布时间】:2012-05-21 15:00:09
【问题描述】:

在 jQuery 网站上Plugins/Validation/Methods/digits 有例子:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      digits: true
    }
  }
});

我想确定功能是否需要验证。 我只有 digits 有问题,特别是当我尝试下一步时:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      digits: function() { return false; }
    }
  }
});

验证插件仍然只需要我的输入字段的数字。 如何在jQuery validate插件中通过函数切换digits验证器?

【问题讨论】:

  • 我下次尝试时你是什么意思????
  • 我想确定函数是否需要验证。

标签: jquery jquery-plugins jquery-validate


【解决方案1】:

有点晚了,但以防万一你想永久替换默认的“必需”功能 - 你可以这样做 -

 $.validator.addMethod("required", function(value, element, param) {
    // your logic here

    // return the result
    return false;
  }, "custom message" /* or the default message - jQuery.validator.messages.required */
  );

【讨论】:

    【解决方案2】:

    我认为你需要

    digits: {
    depends: function(){
          return false;                            
       }
    }
    

    试试这个

    $("#myform").validate({
      rules: {
        field: {
          required: true,
              digits: {
             depends: function(){
                     return (some_condition());
                 }
             }
          }
      }
    });
    

    【讨论】:

    • 我想确定功能是否需要验证。
    • 不客气 :D,如果这是正确答案,请标记为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2011-04-01
    相关资源
    最近更新 更多