0

I want to be able to allow 0 or 0.0 or 100.20 or 1000 or 0.99

I want to enter 4 digit+decimal point+2 digit

Where decimal & 2 digit are not mandatory & out of 4 digits I want to allow 1 digit too

I want the exact regex to match this.

Francis Gilbert
  • 3,382
  • 2
  • 22
  • 27
MayureshP
  • 2,514
  • 6
  • 31
  • 41
  • Maybe related: http://stackoverflow.com/questions/354044/what-is-the-best-u-s-currency-regex/354365#354365 – BrunoLM Jun 13 '11 at 14:26

1 Answers1

3
/\d{1,4}(\.(\d{1,2}))?/

This says "one to four digits optionally followed by a . and one or two digits".

Surround that with \b if you want to match (and extract in the following) a word within a string:

/\b(\d{1,4}(\.(\d{1,2}))?)\b/

If you want the string to match strictly that pattern and nothing else (i.e. nothing before or after), you can anchor it with ^ and $:

/^\d{1,4}(\.(\d{1,2}))?$/
Mat
  • 202,337
  • 40
  • 393
  • 406