使用 bassistance 的 jQuery 验证插件,如何以编程方式验证表单而不触发错误消息?

发布于 2024-11-08 11:54:15 字数 644 浏览 0 评论 0原文

问题有点复杂。让我用最终目标来回答这个问题。

目标
我想运行某种代码,使用验证插件检查表单的有效性,但不会触发错误消息。我希望表单的提交操作遵循相同的规则,但随后抛出错误。

基本上,我想禁用“提交”按钮,直到表单完全有效。因此,每当字段发生更改时,我都需要检查是否满足所有规则。此过程会触发显示错误。这(就我而言)是有问题的。

问题
有谁知道使用验证插件检查表单有效性的方法,以便在表单上的任何内容发生更改时仅返回布尔值 true 或 false,而不会触发错误消息?

我尝试过的
我尝试过使用 success 方法,但是这只在提交表单后才起作用,并且它只直接影响更改的字段,而不是整个表单。

我还尝试使用 $('#start_date').valid() ,但是正如我上面所说,这会触发错误显示在表单上。

有什么想法吗?

插件(这样就不会混淆):bassistance 的 jQuery 验证

The question is a bit complicated. Let me preface this question with the end goal.

Goal
I would like to run some kind of code that will check the validity of the form using the validate plug-in but not trigger the errors messages. I would like the Submit action of the form to follow the same rules, but then throw the errors.

Basically, I want to disable the Submit button until the form is completely valid. Therefore, anytime a field is changed, I need to check to see all the rules are satisfied. This process triggers the errors to be shown. This (in my case) is problematic.

Question
Does anyone know of a way to check the validity of the form using the Validation plug-in to just return a boolean true or false without triggering the errors messages any time anything on the form is changed?

What I've Tried
I have tried using the success method, however this only works after the form has been submitted and it only directly affects the field that was changed, not the form as a whole.

I've also tried to use $('#start_date').valid(), however as I have said above, this triggers the errors to display on the form.

Any ideas?

The plug-in (so there's no confusion): bassistance's jQuery Validation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-11-15 11:54:15

我能想到的最快方法是使用自定义验证错误消息放置函数,如果您试图避免触发错误,则该函数不会执行任何操作。因此,

var hideerrors = false;
form.validate({ errorClass: "err",
            errorPlacement: function(error, element) {
                        if(hideerrors) return;
            error.appendTo(element.parent("div").next("div.errlabel"));
            }
        });

当您想要验证时,只需将 hideerrors 设置为 true,调用 validate,然后再次将 hideerrors 设置为 false。

The quickest way I could think of would be to use a custom validation error message placement function that did nothing if you were trying to avoid triggering the errors. So something like:

var hideerrors = false;
form.validate({ errorClass: "err",
            errorPlacement: function(error, element) {
                        if(hideerrors) return;
            error.appendTo(element.parent("div").next("div.errlabel"));
            }
        });

Then when you want to validate you can just set hideerrors to true, call validate, then set hideerrors to false again.

北恋 2024-11-15 11:54:15

昨晚我深入研究了该插件的实际源代码,实际上发现了一个未记录的方法,它正是我想要的!

$('#formname').validate().checkForm() - 根据表单是否有效返回 true 或 false,而不触发错误消息。

我不知道为什么这个方法没有记录,但它确实应该记录。

我的禁用或启用提交按钮的实现是这样的:

$('#formname').bind('change keyup', function() {
        if($(this).validate().checkForm()) {
            $('#submitbutton').removeClass('button_disabled').attr('disabled', false);
        } else {
            $('#submitbutton').addClass('button_disabled').attr('disabled', true);
        }
    });

I dove into the actual source code of the plug-in last night and actually found an undocumented method that does exactly what I want!

$('#formname').validate().checkForm() - Returns true or false based on whether or not the form is valid, without triggering the error messages.

I don't know why this method isn't documented, but it really should be.

My implementation of it to disable or enable the Submit button is working like this:

$('#formname').bind('change keyup', function() {
        if($(this).validate().checkForm()) {
            $('#submitbutton').removeClass('button_disabled').attr('disabled', false);
        } else {
            $('#submitbutton').addClass('button_disabled').attr('disabled', true);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文