2

I need number validation for String (i.e) type="text"

<input type="text" min="1" max="5" ng-pattern="/^[0-9]+$/" required />

5
  • What is wrong with your posted code ? I mean what change you want ? Commented Oct 19, 2016 at 7:13
  • If i give type="text" number validation is not working. I want to save the value as a string Commented Oct 19, 2016 at 7:17
  • What do you mean by not working ? Is it not validating that only numbers are present or you want users to not to be able to type any other character into field ? Commented Oct 19, 2016 at 7:19
  • The user able to give character as input. But i want user to give only numbers without changing my input type="text" Commented Oct 19, 2016 at 7:23
  • 1
    You are going to need a function to handle all case perfectly. Search it and you will find many threads on it. Commented Oct 19, 2016 at 7:28

2 Answers 2

4

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 />
Sign up to request clarification or add additional context in comments.

6 Comments

This will prevent input from other keys like backspace, enter, delete so not so usable practically since user cant delete the value or edit it. Only option will be up and down button inside the field
@sanjan it can be edit the value please check it while i am using type="text"
I want user to give ranges from 0-100
you change type="number " then give min="1" and max="100" , if submit your form its through the validation error. only valid for 1-100
<input type="number" min="1" max="100" onkeypress='return event.charCode >= 48 && event.charCode <= 57' required />
|
3

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>

1 Comment

No i want it for for sting <b>type="text"<b>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.