Issue
I am doing some validations in my form using AngularJS. In that there are a few fields where I need to accept only numbers. The range is from a single digit to n digits. I am planning to using regex in ngPattern
attribute of AngularJS.
I need to accomplish the following things using the regex:
- If it's a single digit, it should not be a 0. Can be anything between 1 to 9.
- If it's a multi digit number, all numbers from 0 to 9 can be used in any position where not all numbers are 0 i.e. the total value of the input should not be 0
I am struggling to find a regex that will accept such pattern. Is it possible to accept such pattern using regex? Earlier I was thinking that at least the last number should be non-zero digit so that entries like 0000 will not be possible and will have to be 000x where x is anything from 1 to 9. But then this logic is flawed as it wont allow people to input values in multiples of 10.
How do I implement this validation?
Solution
I think, this is the one you're looking for:
^[0-9]*[1-9][0-9]*$
It matches on any number that contains at least one non-zero digit.
Answered By - Adam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.