简体   繁体   English

jQuery Validation插件 - 根据类属性关闭验证

[英]jQuery Validation plug-in – turning off validation based on class attributes

How can I instruct jQuery Validation plug-in to turn off validation based on class attributes and just work based on json rules? 如何指示jQuery Validation插件根据类属性关闭验证并根据json规则工作?

This is conflicting with my jQuery templating system. 这与我的jQuery模板系统相冲突。

First, in your JQuery ready function set the class to 'ignore' or 'require' for each element: 首先,在您的JQuery ready函数中,将每个元素的类设置为'ignore'或'require':

$("#name").attr("class", "required");
$("#email").attr("class", "ignore");

Second, set validate function to ignore the 'ignore' class: 其次,设置validate函数以忽略'ignore'类:

$("#form1").validate({ignore: ".ignore"});

This will require the name but ignore the email. 这将需要名称但忽略电子邮件。

Looking at the source of jquery.validate.js, there is a variable called classRuleSettings, which stores the validation types based on class names. 查看jquery.validate.js的源代码,有一个名为classRuleSettings的变量,它根据类名存储验证类型。 Set it to a 0 length array and you shold be good. 将它设置为0长度数组,你应该很好。

Try this(): 尝试这个():

<form>
    <input type="text" class="required"/>
    <input type="Submit"/>
</form>
<script type="text/javascript">
  $.validator.classRuleSettings = []; //I am not sure about any side effects because of this
  $("form").validate();
</script>

You can use ignore option 您可以使用ignore选项

$("form").validate({ignore:".ignore"});

<input type="text" id="fName" class="required ignore"/>  <!--fname won't be "required"-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM