jQuery验证自定义规则 [英] jQuery validation custom rule

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

问题描述

我为jQuery验证插件制定了自定义规则,并且希望将其应用于不需要的字段.因此,当我在字段中输入内容时,应进行检查,但如果为空,则不应通过自定义规则进行检查.

I made a custom rule for the jQuery validation plugin and I want it to be applied to a field that is not required. So, when I enter something in the field it should be checked but if it's empty it shouldn't be checked by the custom rule.

如果我没有将required设置为true并检查此值.optional(element)我会得到依赖项不匹配.

If I don't set required to true and check this.optional(element) i get dependency-mismatch.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以尝试一下. 最初定义所需的规则.然后,您可以按如下所示添加或删除规则.

You can try this. Initially define the rule for required. And then you can add or remove the rule as follows.

str=$("#theFeildId").text();

if(str==""){ 
   $("#theFeildId").rules("remove", "required");
   $("#theFeildId").removeClass("error");
}
else{
    $("#theFeildId").rules("add", "required");
}

这篇关于jQuery验证自定义规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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