Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a405c6c

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 #9874 Closes #9995
1 parent fbad280 commit a405c6c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ng/directive/input.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,16 @@ 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+
22822292
it('should listen on ng-maxlength when maxlength is observed', function() {
22832293
var value = 0;
22842294
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)