Ramya
Ramya

Reputation: 71

Number validation for a String using ng-pattern

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

Answers (2)

Murali
Murali

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

Anil Kumar Ram
Anil Kumar Ram

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

Related Questions