22

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.

CBuzatu
  • 745
  • 5
  • 16
  • 29

4 Answers4

35

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}$/;
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
2

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

Darshana
  • 2,462
  • 6
  • 28
  • 54
2

Try this.

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

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

Vishak Kavalur
  • 449
  • 1
  • 5
  • 12
2

This works:

/^([0-9]{0,1}([1-9][0-9]){0,2})$/
glennsl
  • 28,186
  • 12
  • 57
  • 75
Ozz
  • 21
  • 1