jQuery 验证插件:默认在 blur() 上验证

jQuery validate plugin: validate on blur() by default

好的,所以我知道 jQuery Validate is opinionated 关于何时(不)在 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

有没有办法覆盖此行为?我希望每个字段在失去焦点时都得到验证,而不仅仅是在出现错误之后。

jQuery 验证默认为 "Lazy" 验证。这意味着,在第一次单击提交之前,在从一个字段移动到另一个字段时会忽略大部分验证。

您正在请求 "Eager" 验证,因此只需使用自定义函数覆盖 onfocusout 默认值...

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

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

其他参考资料

默认onfocusout函数:

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