简体   繁体   中英

Regex for dollar currency format

I need a regex to check if the value is valid dollar currency

eg 12,123,212.00

  • It should accept "," as per the dollar format XXX,XXX,XXX
  • Should accept only one decimal, only two digit after decimal
  • Value can also be without "," eg 12123212.00

You can try this...

 ^\$?((\d{1,3})(?:,[0-9]{3}){0,1}|(\d{1})(?:,[0-9]{3}){0,2}|(\d{1,7}))(\.\d{1,2})?$

If you really want to use regex to validate your input then you may consider:

/^\$?\d{1,3}(?:,\d{3})*(?:\.\d{0,2})?$/gm

RegEx Demo

Other answers miss your requirement Value can also be without "," eg 12123212.00

So I think you should go with:

^(\d+|\d{1,3}(,\d{3})+)\.\d{2}$

Demo: https://regex101.com/r/FkWLjr/3

Feel free to overload the syntax with non capturing groups if the capturing groups bother you.

If you want to allow amounts without decimal parts, just change \.\d{2}$ to (\.\d{2})?$ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM