0

I would like a salary field that only takes numbers and also numbers with maximum 2 digits after the dot. Spaces and commas are not allowed.

My RegEx below is not working it allows spaces and commas and it allows more than 2 digits after the comma

("^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.'][0-9]{2})?|(?:'[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:'[0-9]{2})?)$");

The following is what should be accepted

10000
10000.00

The following should not be accepted

10,000
10'000
10000.456
1000.47888
10 000
polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79
db407
  • 1
  • 1
    Should be accepted `#` or `#.#` ? Why is that ? An input field should accept `#`, `#.#`, `#.`, `.#`. What is a user to think if he can't enter `10.` but can enter `10.0` ? He's gonna think the programmer is lame. –  Jun 25 '17 at 18:08

3 Answers3

0

This was already answered here: Regex currency validation Solution is for JavaScript, but a regex can be generally used in every language

Schlumpf
  • 358
  • 1
  • 3
  • 13
0

Try the following:

^\d+(?:\.\d{1,2}){0,1}$

Explanation:

\d+ - one or more digits

(?:\.\d{1,2}){0,1} - optional (happening once or not at all) dot followed by one or two digits

0

This should do the trick:

^\d+(?:\.\d{1,2})?$

REGEX101 DEMO

Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36