jquery.validate plugin - how to trim values before form validation

前端 未结 16 713
野趣味
野趣味 2020-12-04 16:12

I\'m using the excellent jquery.validation plugin by Jörn Zaefferer and I was wondering whether there\'s a easy way to automatically trim form elements before they are valid

16条回答
  •  囚心锁ツ
    2020-12-04 16:57

    In jquery validation you will find the below code:

    required: function( value, element, param ) {
    
            // Check if dependency is met
            if ( !this.depend( param, element ) ) {
                return "dependency-mismatch";
            }
            if ( element.nodeName.toLowerCase() === "select" ) {
    
                // Could be an array for select-multiple or a string, both are fine this way
                var val = $( element ).val();
                return val && val.length > 0;
            }
            if ( this.checkable( element ) ) {
                return this.getLength( value, element ) > 0;
            }
            return value.length > 0;
        }
    

    Change the value.length to $.trim(value).length

提交回复
热议问题