Currency Validation Regex (USD)
• CronOS Team
regexcurrencyvalidationtutorialusdmoney
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Currency Validation Regex (USD)
Validate US Dollar currency format with optional dollar sign, comma separators, and two decimal places using regex pattern.
Pattern Breakdown
regex
^\$?(\d{1,3}(,\d{3})*|(\d+))(\.\d{2})?$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
\$? | Optional dollar sign | Optional $ symbol |
(\d{1,3}(,\d{3})*|\d+) | Amount | Either comma-formatted or plain number |
\d{1,3}(,\d{3})* | Comma-formatted | 1-3 digits followed by comma groups |
|\d+ | OR plain number | Simple number without commas |
(\.\d{2})? | Optional cents | Optional dot and exactly 2 digits |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
\$?- Optional dollar sign (escaped$)(\d{1,3}(,\d{3})*|(\d+))- Amount alternation:\d{1,3}(,\d{3})*- Comma-formatted: 1-3 digits, then groups of 3 digits with commas|(\d+)- OR plain number without commas
(\.\d{2})?- Optional cents: dot and exactly 2 digits
Examples
Valid:
$100$1,000$1,234.561001,0001234.56$0.50$1,000,000.00
Invalid:
$1,23(incorrect comma placement)$1,234.5(only one decimal place)$1,234.567(three decimal places)$$100(double dollar sign)100.5(one decimal place)1,00(incorrect comma format)
Implementation
JavaScript
javascript
const currencyRegex = /^\$?(\d{1,3}(,\d{3})*|(\d+))(\.\d{2})?$/;
currencyRegex.test('$1,234.56'); // true
currencyRegex.test('100'); // true
currencyRegex.test('$1,000'); // true
currencyRegex.test('$1,234.5'); // false (one decimal place)
currencyRegex.test('$1,23'); // false (incorrect comma)
Python
python
import re
currency_regex = r'^\$?(\d{1,3}(,\d{3})*|(\d+))(\.\d{2})?$'
bool(re.match(currency_regex, '$1,234.56')) # True
bool(re.match(currency_regex, '100')) # True
bool(re.match(currency_regex, '$1,234.5')) # False (one decimal place)
Go
go
currencyRegex := regexp.MustCompile(`^\$?(\d{1,3}(,\d{3})*|(\d+))(\.\d{2})?$`)
currencyRegex.MatchString("$1,234.56") // true
currencyRegex.MatchString("100") // true
currencyRegex.MatchString("$1,234.5") // false (one decimal place)
Limitations
- No negative amounts: Doesn't support negative currency
- Fixed decimal places: Requires exactly 2 decimal places if decimal is present
- US format only: Uses comma for thousands, dot for decimal
- No currency code: Doesn't validate currency codes (USD, EUR, etc.)
- Format only: Validates format, not actual monetary value
When to Use
- USD currency input validation
- Price input forms
- Financial application inputs
- When you need US currency format
- E-commerce price validation
For production, consider:
- Supporting negative amounts if needed:
^-?\$?(\d{1,3}(,\d{3})*|(\d+))(\.\d{2})?$ - Making cents optional:
(\.\d{1,2})? - Supporting different locale formats
- Validating against maximum amounts
- Using currency formatting libraries
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!