I want to validate the contents of a quantity field using Javascript’s Regular Expressions.
Valid input would be an integer number, from zero upwards. If leading zeros could be removed too that would be great.
Examples
1 Pass
0 Pass
01 Failed
00 Failed
-1 Failed
1.1 Failed
1000000 Pass
I have tried myself, but the best I got was…
var regex = /[0-9]{1,9}/;
…which doesn’t fail on negative numbers, or numbers with leading zeros.
Thanks!
This regular expression matches any sequence of digits without a leading
0(except for0itself, which is handled separately).^and$are anchors, which anchor the match to the beginning and end of the string. This means, nothing is allowed before or after the number, and as such, no minus can be included.