You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
input type="number" does not allow numbers with leading decimals on some browsers #5680
The fiddle uses 1.2.1, however, I have reproduced it using 1.2.6
It affects IE9 and Firefox
It does not affect Chrome or Safari
I haven't tested IE8- or IE10+
The desired behavior is to allow the leading decimal to be entered, but do not update the model or set the input element's validation to valid until a valid number is entered.
Users can enter numbers with leading decimals by entering the number first, then scrolling back and entering the decimal.
Instead of modifying the angular source, I'm using a custom validator that I attach to number fields to work around this issue. This appears to consistently mark the field as invalid until a valid number is entered, cross-browser. This also seems to address the issue in IE9 where leading decimals are removed.
After closer inspection, the above code doesn't solve additional problems I'm seeing in IE10+. I just ended up contributing my own number validator directive and switched to a text input. This appears to be the only obvious way to properly validate numbers cross-browser.
The commit is in a private project, but I'll post it below. I wrote a few unit tests that seem to verify it, and some brief cross-browser testing looks good:
describe('number validator',function(){var$scope,$compile,el;beforeEach(function(){module('module');module('test.templates');inject(function(_$rootScope_,_$translate_,_$compile_){$scope=_$rootScope_.$new();$compile=_$compile_;$scope.test={num: null};el='<input type="text" ng-model="test.num" number-validator>';el=$compile(el)($scope);$scope.$digest();});});it('declares valid numbers to be valid',function(){el.val('1').trigger('input');expect(el.hasClass('ng-valid')).toBe(true);el.val('.1').trigger('input');expect(el.hasClass('ng-valid')).toBe(true);el.val('1.0').trigger('input');expect(el.hasClass('ng-valid')).toBe(true);el.val('-1.0').trigger('input');expect(el.hasClass('ng-valid')).toBe(true);});it('declares invalid numbers to be invalid',function(){el.val('-').trigger('input');expect(el.hasClass('ng-invalid')).toBe(true);el.val('.').trigger('input');expect(el.hasClass('ng-invalid')).toBe(true);el.val('abc').trigger('input');expect(el.hasClass('ng-invalid')).toBe(true);});});
I had a similar problem and update the input[type="number"] example on angular docs for works with decimals precision and I'm using this approach to solve it.
Activity
bug($ng): change numberInputType to check for leading decimals
rnicholus commentedon Apr 9, 2015
Instead of modifying the angular source, I'm using a custom validator that I attach to number fields to work around this issue. This appears to consistently mark the field as invalid until a valid number is entered, cross-browser. This also seems to address the issue in IE9 where leading decimals are removed.
Use:
I'm not sold on the directive name though. Oh well.
rnicholus commentedon Apr 10, 2015
After closer inspection, the above code doesn't solve additional problems I'm seeing in IE10+. I just ended up contributing my own number validator directive and switched to a text input. This appears to be the only obvious way to properly validate numbers cross-browser.
joeenzminger commentedon Apr 10, 2015
Ray, can you reference the commit with your number validator? I'd like to take a look at it.
rnicholus commentedon Apr 10, 2015
The commit is in a private project, but I'll post it below. I wrote a few unit tests that seem to verify it, and some brief cross-browser testing looks good:
rnicholus commentedon Apr 10, 2015
Here are my tests:
stewones commentedon Sep 1, 2015
+1
willmendesneto commentedon Apr 6, 2016
I had a similar problem and update the
input[type="number"]example on angular docs for works with decimals precision and I'm using this approach to solve it.Here is the plunker with this snippet