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 though it is not in the range i have declared.
this is my input html:
<input style="width:40%;" type="text" name="phone" ng-minlength="10" ng-maxlength="15" ng-model="register.phone" placeholder='cell phone' ng-pattern="onlyNumbers" required title="please enter cell phone">
can someone help please?
The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ngModel on input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ngpattern attribute value.
The ng-app directive defines the root element of an AngularJS application. The ng-app directive will auto-bootstrap (automatically initialize) the application when a web page is loaded.
To make it simpler \d
= any numeric value
$scope.onlyNumbers = /^\d+$/;
Example: http://jsfiddle.net/TheSharpieOne/JPkER/1/
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]+$/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With