Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
^Start anchorEnsures match from string start
\$?Optional dollar signOptional $ symbol
(\d{1,3}(,\d{3})*|\d+)AmountEither comma-formatted or plain number
\d{1,3}(,\d{3})*Comma-formatted1-3 digits followed by comma groups
|\d+OR plain numberSimple number without commas
(\.\d{2})?Optional centsOptional dot and exactly 2 digits
$End anchorEnsures 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.56
  • 100
  • 1,000
  • 1234.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

  1. No negative amounts: Doesn't support negative currency
  2. Fixed decimal places: Requires exactly 2 decimal places if decimal is present
  3. US format only: Uses comma for thousands, dot for decimal
  4. No currency code: Doesn't validate currency codes (USD, EUR, etc.)
  5. 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!

Generate Regex Pattern