Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max 3 digits, up to 3 decimals

It seems I'm stuck again with a simple regex.

What I'd like:

  • A number between 1 and 999
  • Optional: a comma , sign
  • If the comma sign is entered, minimum 1 decimal and maximum 3 decimals should be presebt.

Allowed:
100
999,0
999,999
999,99

Disallowed:
-1
0
999,
999,9999

This is what I have so far:

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

Any tips?

like image 394
Bv202 Avatar asked Jun 05 '15 08:06

Bv202


1 Answers

You can use this regex:

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

RegEx Demo

Main difference from OP's regex is use of [1-9] which matches digits 1 to 9 before rest of the regex.

like image 194
anubhava Avatar answered Sep 20 '22 08:09

anubhava