CBuzatu
CBuzatu

Reputation: 785

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.

Upvotes: 23

Views: 45790

Answers (4)

Ozz
Ozz

Reputation: 21

This works:

/^([0-9]{0,1}([1-9][0-9]){0,2})$/

Upvotes: 2

Vishak Kavalur
Vishak Kavalur

Reputation: 459

Try this.

/^[0-9]|[0-9][0-9]$/

This should do the job. Using an Or operator does it.

Upvotes: 2

Darshana
Darshana

Reputation: 2548

try this regex: /^[1-9]\d{0,1}$/

Upvotes: 2

Joel Etherton
Joel Etherton

Reputation: 37543

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

Upvotes: 37

Related Questions