Skip to content

Commit 11ed50a

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 f3469a8 commit 11ed50a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/ng/directive/input.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2630,9 +2630,10 @@ var maxlengthDirective = function() {
26302630
link: function(scope, elm, attr, ctrl) {
26312631
if (!ctrl) return;
26322632

2633-
var maxlength = 0;
2633+
var maxlength = -1;
26342634
attr.$observe('maxlength', function(value) {
2635-
maxlength = int(value) || 0;
2635+
var intVal = int(value);
2636+
maxlength = isNaN(intVal) ? -1 : intVal;
26362637
ctrl.$validate();
26372638
});
26382639
ctrl.$validators.maxlength = function(modelValue, viewValue) {

test/ng/directive/inputSpec.js

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

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

@@ -2336,6 +2346,27 @@ describe('input', function() {
23362346
expect(inputElm).toBeValid();
23372347
});
23382348

2349+
it('should accept values of any length when maxlength is non-numeric', function() {
2350+
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
2351+
changeInputValueTo('aaaaaaaaaa');
2352+
2353+
scope.$apply('maxlength = "5"');
2354+
expect(inputElm).toBeInvalid();
2355+
2356+
scope.$apply('maxlength = "abc"');
2357+
expect(inputElm).toBeValid();
2358+
2359+
scope.$apply('maxlength = ""');
2360+
expect(inputElm).toBeValid();
2361+
2362+
scope.$apply('maxlength = null');
2363+
expect(inputElm).toBeValid();
2364+
2365+
scope.someObj = {};
2366+
scope.$apply('maxlength = someObj');
2367+
expect(inputElm).toBeValid();
2368+
});
2369+
23392370
it('should listen on ng-maxlength when maxlength is observed', function() {
23402371
var value = 0;
23412372
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)