Reputation: 71
I need number validation for String (i.e) type="text"
<input type="text" min="1" max="5" ng-pattern="/^[0-9]+$/" required />
Upvotes: 2
Views: 1008
Reputation: 324
Try this code, can't type string.Its only allowed the numbers
<input type="text" min="1" max="5" onkeypress='return event.charCode >= 48 && event.charCode <= 57' required />
Upvotes: 4
Reputation: 1221
Just use input type="number"
<input type="number" min="1" max="5" ng-pattern="/^[0-9]+$/" required />
as per your requirement for input type="text" check the snippet
function myCtrl($scope) {
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="myCtrl">
<div ng-form="myForm">
<input type="text" ng-model="image" min="1" max="5" name="textfrom" required ng-pattern="/^[0-9]+$/" />
<div>{{myForm.textfrom.$valid}}</div>
</div>
</div>
Upvotes: 3