Shane
Shane

Reputation: 5677

input type number fail's with zero

I have the below input field and a ng-change function, which when triggered is failing when the number starts with zero.

<input type="number" ng-model="num" ng-change="confirm(num, 9)" />

function confirm = function(value, length) {
   if (!value) return false;
   $scope.val =  ("" + value).length !== length;
};

The below number when i enter is failing "011111111"

Upvotes: 2

Views: 1994

Answers (1)

Sterling Archer
Sterling Archer

Reputation: 22405

Mathematically speaking, numbers cannot have leading zeros. In JS, Putting a leading 0 on an integer is the equivalent to parsing octal, or base 8.

var num = 011111111;
//is the same as
parseInt(011111111, 8);
//equals 2396745

When in reality you want a decimal parse, which is base 10. To keep your leading zero, you'll have to make sure the input is a string, or remove it and do your own parsing to add it in there if detected.

Upvotes: 2

Related Questions