Skip to content Skip to sidebar Skip to footer

Ng-pattern For Only Numbers Will Accept Chars Like '-' In Angular.js

pattern to make an input text accept only numbers between 0-9. this is my pattern: $scope.onlyNumbers = /[\u0030-\u0039]+/g; for some reason, chars like '-' will be accepted even

Solution 1:

To make it simpler \d = any numeric value

$scope.onlyNumbers = /^\d+$/;

Example: http://jsfiddle.net/TheSharpieOne/JPkER/1/

Solution 2:

You need to anchor the expression to restrict the value to numbers only and I don't think you need the global modifier so you expression should become:

$scope.onlyNumbers = /^[\u0030-\u0039]+$/;

And even a better expression:

$scope.onlyNumbers = /^[0-9]+$/;

Solution 3:

Try this for greater than 0 numbers: $scope.onlyNumbers = /^[1-9]+[0-9]*$/ For any numbers: `$scope.onlyNumbers = /^[0-9]+$/'

Solution 4:

You need to edit the regx pattern

$scope.onlyNumbers = /^[0-9]{1,7}$/;

Post a Comment for "Ng-pattern For Only Numbers Will Accept Chars Like '-' In Angular.js"