Developer
Developer

Reputation: 769

Regex for decimal input in Javascript having 1 to 3 digits before decimal and 1 to 3 digits after decimal

I wanted to create a Javascript regex, which accepts

123, 123.123, 12.34, 1.324

But should not accept

1246, 1234.45, 1.2364

Upvotes: 0

Views: 1679

Answers (3)

tuomassalo
tuomassalo

Reputation: 9101

Try this one:

/^(0|[1-9]\d?\d?)(\.\d{1,3})?$/

I assume "00" or "01.234" should not be valid. Use the other answers if they should. :)

Upvotes: 0

devnull69
devnull69

Reputation: 16544

Try this

/^\d{1,3}(\.\d{1,3})?$/

Upvotes: 0

Danil Speransky
Danil Speransky

Reputation: 30453

Try this regexp:

/^[0-9]{1,3}(\.[0-9]{1,3})?$/

Upvotes: 2

Related Questions