CBuzatu CBuzatu - 4 months ago 8
Javascript Question

One or two numeric digits Regex

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

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}$/;