jQuery 在单个字段上禁用规则验证 [英] jQuery disable rule validation on a single field
问题描述
我正在使用 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.
有人有这方面的经验吗?
Anyone has any experience with that?
推荐答案
只需添加忽略规则并定义选择器即可.在此示例中,验证将忽略具有 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屋!