Jquery Validation Plugin - can you enable “eager” validation from the options?

后端 未结 4 513
挽巷
挽巷 2020-12-29 12:27

I\'m using the Jquery Validation plugin on a project.

By default, the plugin validates input when the submit button is clicked. The behavior is \"lazy\" in order to

4条回答
  •  爱一瞬间的悲伤
    2020-12-29 13:16

    I wanted to add to Robert's answer that you can do this in the default settings by using setDefaults() so it doesn't rely on tapping into the actual instance of the validate method (which is not typically exposed with unobtrusive validation).

    Just override the onfocusout property with a function that always evaluates the blurred element by calling the valid() method:

    // enable eager evaluation
    $.validator.setDefaults({
        onfocusout: function (element) {
            $(element).valid();
        }
    })
    

    Demo With Eager Evaluation:
    (Will show required after tab out)

    // enable eager evaluation
    $.validator.setDefaults({
      onfocusout: function (element) {
        $(element).valid();
      }
    });
    
    $("form").validate();
    input.error {
      border-color: red
    }
    
    
    
    


    Normal Demo
    (Won't show required until submit)

    $("form").validate();
    input.error {
      border-color: red
    }
    
    
    
    


提交回复
热议问题