Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`ng-pattern` - how to check for only numbers?

I am using an ng-pattern to check for ONLY numbers in my input. But for me that's not working. I can able to type in characters after the number. How can I restrict that?

here is my code :

<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
    <input type="text" ng-model="price" name="price_field" ng-pattern="/^[0-9]/" required>
    <span ng-show="myForm.price_field.$error.pattern">Not a valid number!</span>
    <input type="submit" value="submit"/>
</form>
</div>

Live Demo

like image 986
user2024080 Avatar asked Dec 23 '15 06:12

user2024080


1 Answers

Use this expression

ng-pattern="/^[0-9]*$/"

UPDATE :

for accepting the only characters,you can use like this.

ng-pattern="/^[a-zA-Z]*$/"
like image 193
RIYAJ KHAN Avatar answered Oct 12 '22 14:10

RIYAJ KHAN