I have an input field which is number type in my form. It is not a mandatory field. So i have not given required attribute. But whenever the user gives value to it he must provide only numeric value and not alphabets or any special characters. How to validate this in ng-show of angularjs?
-
Check this out : jsfiddle.net/bruscopelliti/3vRXy/lightESD– ESD2014-06-18 15:24:38 +00:00Commented Jun 18, 2014 at 15:24
-
In the fiddle the userid is given as required field. But in my case it is not a mandatory field.Wilson– Wilson2014-06-19 06:25:22 +00:00Commented Jun 19, 2014 at 6:25
Add a comment
|
3 Answers
Please see here for example : http://jsbin.com/renegica/12/
<form name="myForm" ng-submit="onSubmit()">
<input type="text" name="number" ng-model="number" ng-pattern="/^[0-9]+$/" required/>
<span ng-show="myForm.number.$error.pattern">Not a number!</span>
</form>
4 Comments
Wilson
Ya this will work. But at the same time i need to validate the maximum and minimum number if user enters numbers. In that case what i need to do?
sylwester
please see here jsbin.com/renegica/13/edit you can use min and max just you need to change input type from text to number
Wilson
But if i do so then i cannot validate alphabets if i did not give required attribute to number field. i have marked the answer. Now i am using ng-pattern to text field and solved the problem.
sylwester
Yes that's right ng-patter is the best solution and using reg-generator is pretty easy to validate number.
Use angular-ui mask here-> http://angular-ui.github.io/ui-utils/#/mask
Here is an example of my using it with a phone number pattern:
<input name="phoneInputNumber"
ui-mask="(999)999-9999"
ng-model="phoneInput.number" />
Comments
Hope this Thread will solve your problem. Solving a Regex for a number range
You can get your regex pattern for any number range in http://utilitymill.com/utility/Regex_For_Range according to this thread.