I'm writing the validations in AngularJS. I've to allow exactly 6 digit positive number (6 digit whole number). I'm using following regex but it isn't working:
"pattern": /^\+?[0-9]+$/
it is accepting -15 also.
You didn't backslash the first plus sign. In regular expressions a plus sign means that the previous element should be matched one or more times. However, you just want a regular plus, so add a \ to it.
/^\+?([1-9][0-9]{5})$/
If you do more with regular expressions then RegExr.com is a great website where you can learn more and test them in real time.
/^\+?([1-9][0-9]{5})$/match function on it with this pattern. Clearly from RegExr you can see it's working as intendedThe first matches any number of digits within your string. The second allows only 6 digits (and not less). So just take the better from both:
/^\d{1,6}$/
where \d is the same like [0-9].
For example:
var data ='-15';
var reg= /^\d{1,6}$/;
alert(reg.test(data));
/*
1. '-15' will prompt false.
2. '012345' will prompt true.
3. '' will prompt false.
*/
Live example here