jQuery在单个字段上禁用规则验证 [英] jQuery disable rule validation on a single field

查看:71
本文介绍了jQuery在单个字段上禁用规则验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC创建在运行时生成的表单.为了进行验证,我正在尝试使用非常方便的jQuery验证库.我在标签的 cdata 属性中具有每个字段的验证表达式

I am using MVC to create forms that are generated at runtime. For validation, I am trying my hand at the jQuery validation library which is very convenient to use. I have the validation expression of each field in the cdata attribute of the tag

<input type="text" name="xyz" id="xyz" class="defaultTextBox"
  cdata="{validate:{required:true, decimal:true, messages:
          {required:'Please enter an decimal value', 
           decimal:'Please enter a valid decimal'}}}">

这很漂亮.现在,我还有一个要求,就是根据页面上的逻辑显示和隐藏某些字段,并且我需要禁用对隐藏字段的验证,以免它们干扰表单提交.只需将required:true切换为false然后返回true就足够了.只有我不知道.

This works beautifully. Now one more requirement I have is that some fields are being shown and hidden according to the logic on the page and I need to disable the validation on the hidden fields such that they do not interfere with the form submission. Just toggling the required:true to false and back to true should be enough. Only i do not know how.

有人对此有经验吗?

推荐答案

只需添加忽略规则并定义选择器. 在此示例中,验证将忽略所有具有class="ignore"

Just add the ignore rule and define the selector. In this example, the validation will ignore all elements that have the class="ignore"

$("#myform").validate({
   ignore: ".ignore"
})

这篇关于jQuery在单个字段上禁用规则验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆