jQuery validate插件:默认情况下对blur()进行验证 [英] jQuery validate plugin: validate on blur() by default

查看:259
本文介绍了jQuery validate插件:默认情况下对blur()进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我得到了

Okay, so I get that jQuery Validate is opinionated about when (not) to validate on blur:

在将字段标记为无效之前,验证是懒惰的:在首次提交表单之前,用户可以在字段上进行制表,而不会收到烦人的消息-在有机会实际输入密码之前,他们不会被打扰.正确的值

Before a field is marked as invalid, the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages – they won't get bugged before having the chance to actually enter a correct value

是否有一种方法可以覆盖此行为?我希望每个字段失去焦点时都进行验证,而不仅仅是在出现错误之后进行验证.

Is there a way to override this behavior? I want each field to be validated whenever it loses focus, not just after there's an error.

推荐答案

jQuery验证默认为惰性"验证.这意味着,在第一次单击提交之前,从一个字段移到另一个字段时,很多验证都将被忽略.

jQuery Validation is "Lazy" validation by default. This means that, before the submit is clicked the first time, much of the validation is ignored when moving from field to field.

您正在请求急切"验证,因此只需使用自定义功能覆盖onfocusout默认值...

You're requesting "Eager" validation, so simply over-ride the onfocusout default with a custom function...

$('#yourForm').validate({
    // rules, options, etc.,
    onfocusout: function(element) {
        // "eager" validation
        this.element(element);  
    }
});

文档: http://jqueryvalidation.org/validate/#onfocusout

其他参考: https://stackoverflow.com/a/28114269/594235

默认onfocusout功能:

Default onfocusout function:

onfocusout: function(element) {
    // "lazy" validation by default
    if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) {
        this.element(element);
    }
},

这篇关于jQuery validate插件:默认情况下对blur()进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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