Skip to content

Commit 2f02174

Browse files
committed
fix(ngMaxlength): ignore maxlength when not set to a non-negative integer
This makes the behaviour of maxlength/ngMaxlength more inline with the spec. Closes angular#9874
1 parent 11da02a commit 2f02174

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2610,9 +2610,9 @@ var maxlengthDirective = function() {
26102610
link: function(scope, elm, attr, ctrl) {
26112611
if (!ctrl) return;
26122612

2613-
var maxlength = 0;
2613+
var maxlength = -1;
26142614
attr.$observe('maxlength', function(value) {
2615-
maxlength = int(value) || 0;
2615+
maxlength = int(value) || -1;
26162616
ctrl.$validate();
26172617
});
26182618
ctrl.$validators.maxlength = function(modelValue, viewValue) {

test/ng/directive/inputSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,28 @@ describe('input', function() {
22932293
expect(inputElm).toBeValid();
22942294
});
22952295

2296+
it('should accept values of any length when maxlength is non-numeric', function() {
2297+
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
2298+
changeInputValueTo(new Array(1001).join('a'));
2299+
2300+
scope.$apply('maxlength = "abc"');
2301+
expect(inputElm.val().length).toBe(1000);
2302+
expect(inputElm).toBeValid();
2303+
2304+
scope.$apply('maxlength = ""');
2305+
expect(inputElm.val().length).toBe(1000);
2306+
expect(inputElm).toBeValid();
2307+
2308+
scope.$apply('maxlength = null');
2309+
expect(inputElm.val().length).toBe(1000);
2310+
expect(inputElm).toBeValid();
2311+
2312+
scope.someObj = {};
2313+
scope.$apply('maxlength = someObj');
2314+
expect(inputElm.val().length).toBe(1000);
2315+
expect(inputElm).toBeValid();
2316+
});
2317+
22962318
it('should listen on ng-maxlength when maxlength is observed', function() {
22972319
var value = 0;
22982320
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)