Regex for Currency Amounts: The Pattern That Rejects 1,234.567 and Accepts 1,234.56
Regex for currency amounts is a problem that looks solved until you test it against real user input, and real user input has never once respected a tidy pattern.
Remember the string 1,234.56 from the title? That is the easy case, and it is the case every tutorial tests. The hard cases are the ones your users actually type: 1234.5 with one decimal place, 1,234.567 with three, 12,34.56 with a comma in the wrong place, and 1.234,56 from a European user who means something completely different by those same digits. A currency pattern that accepts the first list and rejects the second is doing its job. A pattern that accepts everything with digits and a dot is a formatting function wearing a validation costume.
Start with the strict US dollar pattern, because it teaches the structure everything else builds on: ^\$?([1-9]\d{0,2}(,\d{3})*|0)(\.\d{2})?$ . Read it in pieces. The optional dollar sign comes first, escaped, because an unescaped dollar sign means end of string and will ruin your afternoon. The integer part allows either a properly grouped number like 1,234 or a plain zero, and the grouping rule is the important part: one to three digits, then groups of exactly three. That single rule rejects 12,34 and 1234,567 in one stroke. The decimal part, when present, demands exactly two digits, which rejects 1,234.5 and 1,234.567 alike.
That strictness is a choice, and like most strict choices it has a cost. If your form accepts amounts without cents, the strict pattern will reject a legitimate 500 and annoy a user who typed a perfectly reasonable number. Loosen the decimal part to (\.\d{1,2})? and you accept one or two decimal places, which matches how people actually type money in a form. Loosen it to (\.\d+)? and you have given up entirely, because now any number of decimal places passes and the pattern validates nothing about cents. The art of this pattern is deciding which looseness is a feature and which is a surrender, and that decision belongs to the product, not to the regex.
Regex for currency amounts beyond US dollars
Negatives come in two costumes, and your pattern should pick one deliberately. A leading minus sign is the common form: ^-?\$?([1-9]\d{0,2}(,\d{3})*|0)(\.\d{2})?$ . Parentheses are the accounting form, where (1,234.56) means negative 1,234.56, and that needs a conditional group or two separate patterns, because a pattern that accepts an opening parenthesis without demanding the closing one is a bug. I have seen that bug in production code, and it accepted (1,234.56 with no close, which is not a number in any accounting system I know.
European formats are where a single pattern stops being honest. In much of Europe, 1.234,56 means one thousand two hundred thirty four euros and fifty six cents, with the dot as the thousands separator and the comma as the decimal mark. That is the exact mirror of the US format, and no single pattern can tell which convention a bare number follows. The honest solution is to know the locale before you validate, either from the user account or from an explicit currency selector, and then apply the matching pattern. A pattern that tries to accept both formats at once accepts ambiguous input, and ambiguous money input is a defect, not a feature.
There is a final trap that sits outside the pattern entirely. Regex validates format, not value, and it cannot tell you whether 999,999,999.99 is a plausible price for the thing being sold. Pair the pattern with a numeric range check after parsing, using a decimal type rather than a float, because floats cannot represent most decimal money values exactly and will eventually produce a cent that does not exist. The regex keeps garbage out of the field. The range check and the decimal type keep nonsense out of the ledger. Both jobs matter, and neither does the other job.
The pattern I actually ship
For US dollar forms, I ship ^-?\$?([1-9]\d{0,2}(,\d{3})*|0)(\.\d{1,2})?$ , with a separate range check and a decimal parse, and I test it against a list that includes 0, 0.99, 1,234.56, 12,34.56, 1,234.567, and an empty string, because the empty string is the input that most often slips through an optional everything pattern. If your product serves multiple locales, ship one pattern per locale behind a selector, and never ship a universal currency pattern, because the universe of currency formats does not agree with itself, and your pattern should not pretend otherwise.
Frequently asked questions
What is a good regex for US currency amounts?
A strict pattern is ^\$?([1-9]\d{0,2}(,\d{3})*|0)(\.\d{2})?$ . It enforces proper comma grouping and exactly two decimal places. Loosen the decimal part to (\.\d{1,2})? if your form accepts amounts without cents.
Why does my currency regex accept 12,34.56?
Because the integer part allows any digits without enforcing groups of three. Use ([1-9]\d{0,2}(,\d{3})*|0) so that commas only appear in groups of exactly three digits.
How do I validate negative currency amounts with regex?
Use a leading minus sign, or handle accounting parentheses as a separate case. A pattern that accepts an opening parenthesis without demanding the closing one is a bug.
Can one regex handle both US and European currency formats?
No, not honestly. In European formats 1.234,56 means something different from the US reading of the same digits. Know the locale first, then apply the matching pattern.
Should I use regex alone to validate money?
No. Regex validates format, not value. Pair it with a numeric range check after parsing, using a decimal type rather than a float, because floats cannot represent most decimal money values exactly.
Related reading: Regex for Postal Code Validation · Email Regex Validation Mistakes · Regex URL Validation Mistakes · Regex for Credit Card Number Validation