使用jQuery验证忽略某些验证规则 [英] Ignoring certain validation rules with jQuery validate

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

问题描述

我正在使用jQuery validate插件在我的表单上进行自定义验证。对于一种特定类型的表单,用户可以选择提交表单,或者他们也可以保存表单而不提交。我能够使用 $('form.validate')。validate()。cancelsubmit = true; 来禁止onClick处理程序中的保存按钮验证,生命是好的。

I am using jQuery validate plugin to do custom validations on my forms. For one particular type of form, the user has the option to submit the form, or they can also save the form without submitting. I was able to use $('form.validate').validate().cancelSubmit = true; to suppress validation in the onClick handler for the save button, and life was good.

但是,其中一个自定义验证强制要求人们必须输入合法字符(即强制您使用有效的ascii字符)。我想继续强制执行ascii验证,因为如果不这样做,人们就可以将不良数据保存到数据库中(这会导致我们运行的集成混乱)。

However, one of the custom validations that was written enforces that people must enter legit characters (i.e. it enforces that you use valid ascii characters). I want to continue to enforce the ascii validation, because if I don't, people are able to save bad data to the database (which subsequently messes up integrations we have running).

基本上我想强制执行除必需之外的所有规则:true。我看到你可以在表格 $('form.validate')上设置验证时使用ignore选项.validate({ignore:。required}); 但这只在您最初设置验证时才起作用。当我尝试在用户点击按钮时尝试这样做时,它似乎没有做任何事情。

Basically I want to enforce all rules except required: true. I see that you can use the ignore option when setting up the validation on the form $('form.validate').validate({ignore : ".required"}); but this only seems to work when you are initially setting up the validation. When I try to do that when a user clicks the button, it doesn't seem to do anything.

我看过一些关于类似事情的帖子,但通常与忽略隐藏的领域。有人知道我需要使用正确的语法/方法来忽略点击按钮时所需的规则吗?任何帮助将不胜感激。

I've seen some posts about similar things but usually related to ignoring hidden fields. Does anybody know the correct syntax / method I need to use to ignore the required rule upon button click? Any help would be appreciated.

推荐答案

你可以使用 规则('删除')方法动态删除规则。你的保存按钮上有这样的东西。

You could use the rules('remove') method to remove your rules dynamically. Something like this on your "save" button.

$('#save').click(function () {
    $('#myform').find('.myclass').each(function () {
        $(this).rules('remove', 'required');
    });
    // your code to save form
});

演示: http://jsfiddle.net/NqT2V/

重要提示根据规则('删除')文档仅处理通过rules-option指定的规则或通过规则('add')换句话说,这如果您的规则是通过 class =required 内联添加的,则无效。

Important NOTE: As per rules('remove') documentation, "Manipulates only rules specified via rules-option or via rules('add')." In other words, this will not work if your rules are added inline via class="required".

规则('添加')方法到添加规则回归就在之前提交 ...

And the rules('add') method to add the rules back just before submit...

$('#submit').click(function (e) {
    e.preventDefault();
    $('#myform').find('input').each(function () {
        $(this).rules('add', 'required');
    });
    $('#myform').submit();
});

演示: http://jsfiddle.net/3G8cN/

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

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