Javascript Currency Validation

The following will check a string to make sure it is a valid currency. It can only include numbers and a . (decimal point which is optional).

  1. function validateCurrency(amount) {
  2. var regex = /^[1-9]\d*(?:\.\d{0,2})?$/;
  3. return regex.test(amount);
  4. }

The amount must start with a number 1 through 9, you can change this to 0-9 but potentially allows someone to put 0.00 so you would have to apply further validation. Then optionally the user can enter a decimal to 2 decimal places.

This validates for:

  • 20.00
  • 13.12
  • 12.5
  • 1.00
  • 1.50

But fails on:

  • 20.000
  • 0.50
  • 0.2
  • 1,500
  • 1,200.50