G--
G--

Reputation: 23

Regex to allow values 1 to 100 with some conditions

i want a regex to allow 1-3 digits whole number,it should not start with 0 and should be a whole number,not decimals or fractions or any signed values,

this is what i have tried

"/^(\s*|\d{1,3})$/"

but it does not seem to work..

thanks in advance

Upvotes: 2

Views: 469

Answers (2)

unxnut
unxnut

Reputation: 8839

^[1-9][0-9]\{,2\}$

It should be indeed end of line; end-of word will get the fractional part.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

You can do it like this:

^(([1-9]\d?)|100)$

This would ensure that

  • There is at least one digit
  • That multi-digit numbers start in a digit other than zero
  • That the only allowed three-digit number is 100.

Upvotes: 5

Related Questions