Skip to content

Commit 14457df

Browse files
committed
feat(ngMaxlength): add support for disabling max length limit
Previously, setting the maxlength to a negative number, would make all input values invalid (since their length should be less than maxlength, which is impossible). This commit changes the behaviour of maxlength/ngMaxlength, effectively disabling the maxlength validation (always returning true) when maxlength is set to a negative number. This is more inline to how the HTML5 `maxlength` attribute works (both in browsers and according to the spec: http://dev.w3.org/html5/spec-preview/attributes-common-to-form-controls.html#attr-fe-maxlength). Related to angular#9874 Closes angular#9995
1 parent 0462ee6 commit 14457df

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ var maxlengthDirective = function() {
26162616
ctrl.$validate();
26172617
});
26182618
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2619-
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
2619+
return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength);
26202620
};
26212621
}
26222622
};

test/ng/directive/inputSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,20 @@ describe('input', function() {
22792279
expect(inputElm).toBeValid();
22802280
});
22812281

2282+
it('should accept values of any length when maxlength is negative', function() {
2283+
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
2284+
2285+
changeInputValueTo('');
2286+
expect(inputElm).toBeValid();
2287+
2288+
changeInputValueTo('aaaaaaaaaa');
2289+
expect(inputElm).toBeValid();
2290+
2291+
changeInputValueTo(new Array(1001).join('a'));
2292+
expect(inputElm.val().length).toBe(1000);
2293+
expect(inputElm).toBeValid();
2294+
});
2295+
22822296
it('should listen on ng-maxlength when maxlength is observed', function() {
22832297
var value = 0;
22842298
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)