简体   繁体   中英

Regular expression to validate money with minimum value

I am using below regular expression to validate money which works fine.

^\d{1,3}(,\d{3})*$

Now I want to add minimum amount also like minimum amount should be 20,000 Can anyone please help me out.

Fiddle: https://regexr.com/5h5bf

fiddle updated with correct expression

Reading the comments, I'm not sure if regex would be your way forward, yet you seem determined. It seems that you are looking to validate a comma-seperated string that needs to start at 20,000, where each second part of the number is 3 digits long. I came up with:

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

See the online demo


  • ^ - Start string ancor.
  • (?: - Open 1st non-capture group.
    • [2-9]\d - A digit ranging from 2-9 followed by any digit.
    • | - Or.
    • [1-9]\d\d - A digit ranging from 1-9 followed by any two digits.
    • | - Or.
    • [1-9],\d{3} - A digit ranging from 1-9 followed by a comma and any three digits.
    • ) - Close 1st non-capture group.
  • (?: - Open 2nd non-capture group.
    • ,\d{3} - A comma followed by any three digits.
    • )+ - Close 2nd non-capture group and repeat at least once.
  • $ - End string ancor.

As an alternative you could also use lookaheads, eg:

^(?=.{6,})(?!1.{5}$)[1-9]\d?\d?(?:,\d{3})+$

See the online demo


  • ^ - Start string ancor.
  • (?=.{6,} - Positive lookahead for at 6 or more characters.
  • (?.1.{5}$) - Negative lookahead for 1 followed by 5 characters till end string.
  • [1-9]\d?\d? - A digit ranging from 1-9 followed by two optional digits (you could also write [1-9]\d{0,2} ).
  • (?: - Open 2nd non-capture group.
    • ,\d{3} - A comma followed by any three digits.
    • )+ - Close non-capture group and repeat at least once.
  • $ - End string ancor.

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