Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 476a8db

Browse files
committed
fix(NgModelValidators): ensure that number input types render invalid when non-numeric characters are present
input[type="number"] treats non-numeric values as NaN values and these are considered valid when double.parse is used. This commit fixes the validation for the model to properly address the invalid number when a NaN value is detected.
1 parent 6c9f094 commit 476a8db

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/directive/ng_model_validators.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class NgModelNumberValidator implements NgValidatable {
9090
if (value != null) {
9191
try {
9292
num val = double.parse(value.toString());
93+
if (val.isNaN) {
94+
return false;
95+
}
9396
} catch(exception, stackTrace) {
9497
return false;
9598
}

test/directive/ng_model_spec.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ void main() {
8181
expect(inputElement.value).toEqual('');
8282
}));
8383

84+
it('should be invalid when the input value results in a NaN value', inject(() {
85+
_.compile('<input type="number" ng-model="model" probe="p">');
86+
Probe probe = _.rootScope.context['p'];
87+
var ngModel = probe.directive(NgModel);
88+
InputElement inputElement = probe.element;
89+
90+
inputElement.value = 'aa';
91+
_.triggerEvent(inputElement, 'change');
92+
expect(_.rootScope.context['model'].isNaN).toBe(true);
93+
expect(ngModel.valid).toBe(false);
94+
}));
95+
8496
it('should write to input only if value is different', inject((Injector i, AstParser parser) {
8597
var scope = _.rootScope;
8698
var element = new dom.InputElement();

0 commit comments

Comments
 (0)