jQuery validate plugin: validate on blur() by default

前端 未结 1 1770
难免孤独
难免孤独 2020-11-27 08:00

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

Before a field is marked as invalid, the validat

相关标签:
1条回答
  • 2020-11-27 08:35

    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.

    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);  
        }
    });
    

    Documentation: http://jqueryvalidation.org/validate/#onfocusout

    Additional Reference: https://stackoverflow.com/a/28114269/594235

    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);
        }
    },
    
    0 讨论(0)
提交回复
热议问题