0

I need to validate currency values under $100, allowing the user to optionally leave out the decimal place. The dollar sign and comma are not needed (or allowed). Another validation component constrains the upper and lower values, so the regex expression only needs to validate 1 or 2 digits, with an optional decimal and 2 more digits.

These values would match:

10
5
5.00
10.00

These values would not match:

.25
10.1
10.255

What is the optimal regex pattern for this validation?

lynnjwalker
  • 661
  • 5
  • 9
  • 20
  • 1
    Please consider bookmarking the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) for future reference. The items that best address your problem: Common Tasks > Validation > Numeric > [Using regex to validate a number](http://stackoverflow.com/a/4247184/3622940) and [a min-max range](http://stackoverflow.com/a/22131040/3622940). – aliteralmind Dec 30 '14 at 19:16

2 Answers2

1

Something like

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

Regex Demo

  • ^ Anchors the regex at the start of the string

  • d{1,2} matches 1 or 2 digits \d => [0-9]

  • (\.\d{2}) matches . followed by 2 digits

  • ? quantifier, quantifes the decimal part zero or one time

  • $ anchors the regex at the end of the string

nu11p01n73R
  • 24,873
  • 2
  • 34
  • 48
1

[0-9]{1,2}(\.[0-9]{2}) That should do it.

smci
  • 26,085
  • 16
  • 96
  • 138