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

Commit 92f87b1

Browse files
gkalpakpkozlowski-opensource
authored andcommitted
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 #9874
1 parent 5c1fdff commit 92f87b1

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
@@ -2690,9 +2690,10 @@ var maxlengthDirective = function() {
26902690
link: function(scope, elm, attr, ctrl) {
26912691
if (!ctrl) return;
26922692

2693-
var maxlength = 0;
2693+
var maxlength = -1;
26942694
attr.$observe('maxlength', function(value) {
2695-
maxlength = int(value) || 0;
2695+
var intVal = int(value);
2696+
maxlength = isNaN(intVal) ? -1 : intVal;
26962697
ctrl.$validate();
26972698
});
26982699
ctrl.$validators.maxlength = function(modelValue, viewValue) {

test/ng/directive/inputSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,16 @@ describe('input', function() {
24952495
expect(inputElm).toBeValid();
24962496
});
24972497

2498+
it('should only accept empty values when maxlength is 0', function() {
2499+
compileInput('<input type="text" ng-model="value" ng-maxlength="0" />');
2500+
2501+
changeInputValueTo('');
2502+
expect(inputElm).toBeValid();
2503+
2504+
changeInputValueTo('a');
2505+
expect(inputElm).toBeInvalid();
2506+
});
2507+
24982508
it('should accept values of any length when maxlength is negative', function() {
24992509
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
25002510

@@ -2505,6 +2515,27 @@ describe('input', function() {
25052515
expect(inputElm).toBeValid();
25062516
});
25072517

2518+
it('should accept values of any length when maxlength is non-numeric', function() {
2519+
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
2520+
changeInputValueTo('aaaaaaaaaa');
2521+
2522+
scope.$apply('maxlength = "5"');
2523+
expect(inputElm).toBeInvalid();
2524+
2525+
scope.$apply('maxlength = "abc"');
2526+
expect(inputElm).toBeValid();
2527+
2528+
scope.$apply('maxlength = ""');
2529+
expect(inputElm).toBeValid();
2530+
2531+
scope.$apply('maxlength = null');
2532+
expect(inputElm).toBeValid();
2533+
2534+
scope.someObj = {};
2535+
scope.$apply('maxlength = someObj');
2536+
expect(inputElm).toBeValid();
2537+
});
2538+
25082539
it('should listen on ng-maxlength when maxlength is observed', function() {
25092540
var value = 0;
25102541
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)