Skip to content

Commit f3469a8

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 85eb966 commit f3469a8

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2636,7 +2636,7 @@ var maxlengthDirective = function() {
26362636
ctrl.$validate();
26372637
});
26382638
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2639-
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
2639+
return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength);
26402640
};
26412641
}
26422642
};

test/ng/directive/inputSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,16 @@ describe('input', function() {
23262326
expect(inputElm).toBeValid();
23272327
});
23282328

2329+
it('should accept values of any length when maxlength is negative', function() {
2330+
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
2331+
2332+
changeInputValueTo('');
2333+
expect(inputElm).toBeValid();
2334+
2335+
changeInputValueTo('aaaaaaaaaa');
2336+
expect(inputElm).toBeValid();
2337+
});
2338+
23292339
it('should listen on ng-maxlength when maxlength is observed', function() {
23302340
var value = 0;
23312341
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)