Skip to content

Commit b6a0b07

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 b6a0b07

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ng/directive/input.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2610,9 +2610,10 @@ 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+
var intVal = int(value);
2616+
maxlength = isNaN(intVal) ? -1 : intVal;
26162617
ctrl.$validate();
26172618
});
26182619
ctrl.$validators.maxlength = function(modelValue, viewValue) {

test/ng/directive/inputSpec.js

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

2282+
it('should only accept empty values when maxlength is 0', function() {
2283+
compileInput('<input type="text" ng-model="value" ng-maxlength="0" />');
2284+
2285+
changeInputValueTo('');
2286+
expect(inputElm).toBeValid();
2287+
2288+
changeInputValueTo('a');
2289+
expect(inputElm).toBeInvalid();
2290+
});
2291+
22822292
it('should accept values of any length when maxlength is negative', function() {
22832293
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
22842294

@@ -2293,6 +2303,28 @@ describe('input', function() {
22932303
expect(inputElm).toBeValid();
22942304
});
22952305

2306+
it('should accept values of any length when maxlength is non-numeric', function() {
2307+
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
2308+
changeInputValueTo(new Array(1001).join('a'));
2309+
2310+
scope.$apply('maxlength = "abc"');
2311+
expect(inputElm.val().length).toBe(1000);
2312+
expect(inputElm).toBeValid();
2313+
2314+
scope.$apply('maxlength = ""');
2315+
expect(inputElm.val().length).toBe(1000);
2316+
expect(inputElm).toBeValid();
2317+
2318+
scope.$apply('maxlength = null');
2319+
expect(inputElm.val().length).toBe(1000);
2320+
expect(inputElm).toBeValid();
2321+
2322+
scope.someObj = {};
2323+
scope.$apply('maxlength = someObj');
2324+
expect(inputElm.val().length).toBe(1000);
2325+
expect(inputElm).toBeValid();
2326+
});
2327+
22962328
it('should listen on ng-maxlength when maxlength is observed', function() {
22972329
var value = 0;
22982330
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)