Skip to content

Commit

Permalink
fix issue jquery-validation#2047:Empty maxlength fails validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-shu committed Oct 28, 2020
1 parent b9c793c commit b928fbb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core.js
Expand Up @@ -1461,7 +1461,9 @@ $.extend( $.validator, {
// https://jqueryvalidation.org/maxlength-method/
maxlength: function( value, element, param ) {
var length = Array.isArray( value ) ? value.length : this.getLength( value, element );
return this.optional( element ) || length <= param;

// If param is equal to 0, it means maxlength has no limitation
return this.optional( element ) || param === 0 || length <= param;
},

// https://jqueryvalidation.org/rangelength-method/
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Expand Up @@ -211,6 +211,7 @@ <h3></h3>
</ul>
</form>
<form id="form" action="formaction">
<input type="text" name="action" value="" id="emptyText">
<input type="text" name="action" value="Test" id="text1">
<input type="text" name="text2" value="" id="text1b">
<input type="text" name="text2" value="T " id="text1c">
Expand Down
6 changes: 5 additions & 1 deletion test/methods.js
Expand Up @@ -327,8 +327,12 @@ QUnit.test( "maxlength", function( assert ) {
var v = jQuery( "#form" ).validate(),
method = $.validator.methods.maxlength,
param = 4,
e = $( "#text1, #text2, #text3" );
e = $( "#emptyText" );

assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], 0 ), "Valid text input" );

e = $( "#text1, #text2, #text3" );
assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], 0 ), "Valid text input" );
assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
Expand Down

0 comments on commit b928fbb

Please sign in to comment.