0

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?

2
  • Check this out : jsfiddle.net/bruscopelliti/3vRXy/light Commented 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. Commented Jun 19, 2014 at 6:25

3 Answers 3

3

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

4 Comments

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?
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
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.
Yes that's right ng-patter is the best solution and using reg-generator is pretty easy to validate number.
2

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

0

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.

Comments

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.