Making the JQuery Validation plugin evaluation non-lazy

邮差的信 提交于 2020-01-03 04:29:08

问题


According to the documentation for the JQuery validation plugin:

the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages

Is there any way to display the messages as the user tabs through the form?

Cheers


回答1:


You can override the default onfocusout to do more eager/pre-submit validation you want, like this:

$(function() {
  $("form").validate({
    rules: { ...rules... },
    messages: { ...messages... },
    onfocusout: function(element) { $(element).valid(); }
  });
});

The default onfocusout looks like this, disabling the blur validation until after it's been submitted once:

onfocusout: function(element) {
  if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) {
    this.element(element);
  }
}


来源:https://stackoverflow.com/questions/2893768/making-the-jquery-validation-plugin-evaluation-non-lazy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!