How can I enable jquery validation on readonly fields?

前端 未结 4 730
遇见更好的自我
遇见更好的自我 2020-11-30 09:41

Guys from http://jqueryvalidation.org/ just released version 1.13.1. Checking on their website i see this on the changelog:

CORE: * Ignore readonly as well as disabl

4条回答
  •  醉话见心
    2020-11-30 10:41

    You can override the original "elements" function with an implementation which quite similar to the original implementation except for the readonly field handling. This is not an elegant solution, but it does work. If you update your jquery validate library, you also need to recheck your overridden method.
    * UpToDate * See jquery-validate-1.14 changeLog: Revert "Ignore readonly as well as disabled fields." :):)

    $.validator.prototype.elements = function() {
        var validator = this,
        rulesCache = {};
    
        return $( this.currentForm )
        .find( "input, select, textarea" )
        .not( ":submit, :reset, :image, [disabled]") // changed from: .not( ":submit, :reset, :image, [disabled], [readonly]" )
        .not( this.settings.ignore )
        .filter( function() {
            if ( !this.name && validator.settings.debug && window.console ) {
                console.error( "%o has no name assigned", this );
            }
    
            if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
                return false;
            }
    
            rulesCache[ this.name ] = true;
            return true;
        });         
    };
    

提交回复
热议问题