36

Using HTML5 I have an input field that should validate against a dollar amount entered. Currently I have the following markup:

<input type="number" pattern="(\d{3})([\.])(\d{2})">

This works great for an amount that is greater than 100.00 and less than 1,000.00. I am trying to write the pattern (regex) to accept different dollar amounts. Maybe upwards of 100,000.00. Is this possible?

1
  • Some people use commas as decimal separator. Do you need localization? Commented Oct 6, 2015 at 22:56

8 Answers 8

49

The best we could come up with is this:

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

I realize it might seem too much, but as far as I can test it matches anything that a human eye would accept as valid currency value and weeds out everything else.

It matches these:

1 => true
1.00 => true
$1 => true
$1000 => true
0.1 => true
1,000.00 => true
$1,000,000 => true
5678 => true

And weeds out these:

1.001 => false
02.0 => false
22,42 => false
001 => false
192.168.1.2 => false
, => false
.55 => false
2000,000 => false
Sign up to request clarification or add additional context in comments.

6 Comments

@Bartezz Commas are not valid decimal separators for use in programming languages AFAIK. Computers were invented in the U.S., so periods as the decimal separator is the standard on the web.
@mbomb007 there is a big difference between programming language and user input. Since most of the 7 billion people on this planet don't know programming language your vision is more than short sighted. Besides read up on the history of computers. You shouldn't have skipped those history classes or if you didn't at least request your money back...
@Bartezz I've read lots. It was created at Iowa State University. Sure, there wasn't necessarily currency on it yet, but all the major companies like MS and Macintosh as well as W3C (founded at MIT), etc are from the U.S.
FYI in putting into an input tag I had to change the double escapes to single so ... ^\$?(([1-9](\d*|\d{0,2}(,\d{3})*))|0)(\.\d{1,2})?$ other then that it works well
Going with numeric for money values is not the proper way. As the original designers lived on an island called the USoA, thanks mbomb007, you'll have to implement your own version
|
30

If you want to allow a comma delimiter which will pass the following test cases:

0,00  => true
0.00  => true
01,00 => true
01.00 => true
0.000 => false
0-01  => false

then use this:

^\d+(\.|\,)\d{2}$

2 Comments

FYI - the above regex doesnt actually prevent the likes of 10.000 - it should really shorten it to 10.00 but it doesnt.
If you would also like to capture non-decimal values use this: ^\d+((\.|\,)\d{2})?$
6

How about :

^\d+\.\d{2}$

This matches one or more digits, a dot and 2 digits after the dot.

To match also comma as thousands delimiter :

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

1 Comment

I know it's an old question and answer, but you can ensure there are 1-3 decimals are in the finish digit group. ^\d{1,3}(?:,\d{3})*\.\d{2}$
6

Another answer for this would be

^((\d+)|(\d{1,3})(\,\d{3}|)*)(\.\d{2}|)$

This will match a string of:

  • one or more numbers with out the decimal place (\d+)
  • any number of commas each of which must be followed by 3 numbers and have upto 3 numbers before it (\d{1,3})(\,\d{3}|)*

Each or which can have a decimal place which must be followed by 2 numbers (.\d{2}|)

Comments

3

I like to give the users a bit of flexibility and trust, that they will get the format right, but I do want to enforce only digits and two decimals for currency

^[$\-\s]*[\d\,]*?([\.]\d{0,2})?\s*$

Takes care of:

$ 1.
-$ 1.00
$ -1.0
.1
.10
-$ 1,000,000.0

Of course it will also match:

$$--$1,92,9,29.1 => anyway after cleanup => -192,929.10

Comments

2

I'm wrote this price pattern without zero price.

(0\.((0[1-9]{1})|([1-9]{1}([0-9]{1})?)))|(([1-9]+[0-9]*)(\.([0-9]{1,2}))?)

Valid For:

  • 1.00
  • 1
  • 1.5
  • 0.10
  • 0.1
  • 0.01
  • 2500.00

Invalid For:

  • 0
  • 0.00
  • 0.0
  • 2500.
  • 2500.0000

Check my code online: http://regexr.com/3binj

1 Comment

works fine but it is possible to enter the letter 'e'
0

this in my pattern currency '[0-9]+(,[0-9]{1,2})?$' also input type text

valid for:

  • 1
  • 0,01
  • 1,5
  • 0,10
  • 0,1
  • 0,01
  • 2500,00

Comments

-1

Use this pattern "^\d*(\.\d{2}$)?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.