One or two numeric digits Regex

CBuzatu picture CBuzatu · May 27, 2012 · Viewed 34.7k times · Source

I have the below code. It works only when I have 2 digits. If I have 1 digit doesn't work. I want to work in both cases: one or two digit.
var numberRegex = /^[1-9][0-9]$/;
I've tried something like this but unfortunately doesn't work:
var numberRegex = /^[1-9]?[1-9][0-9]$/;
Thanks for support.

Answer

Joel Etherton picture Joel Etherton · May 27, 2012

Try this one out:

/^\d{1,2}$/;

Reading what you have it looks like you don't want to accept numbers like 01.

/^\d{1}|[1-9]\d{1}$/;