How to validate currency with JavaScript regex?

Sometimes, we want to validate currency with JavaScript regex.

In this article, we’ll look at how to validate currency with JavaScript regex.

How to validate currency with JavaScript regex?

To validate currency with JavaScript regex, we use the following regex:

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

The (?=.*?d) part makes sure that the string starts with a number.

^$?(([1-9]d{0,2} makes sure the number starts with 1 to 9.

(,d{3})*) checks that the number has 0 or more groups of 3 digits after the start of the number separated by commas.

We also have d+ to make the 3 digit groups optional.

Finally, we have ?(.d{1,2})?$ to make sure that the number optionally ends with a decimal point and 0 to 2 digits.

Conclusion

To validate currency with JavaScript regex, we use the following regex:

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