Skip to content
Advertisement

Working with RegEx, but I dont know how to approach my issue

^d{1,12}$|(?=^.{1,15}$)^d+.d{1,2}$

This is the expression I currently have.

I want the max limit to be 100 000 000 000 with optional two decimals max, but if the user only adds 1 decimal, they can bump the value to 100 000 000 0001.1 if they want to.

How can I approach this issue? and is there any way to make 100 000 000 000 the max value? (Not 999 999 999 999)

Advertisement

Answer

You can use the following regex to match numbers from 0 to 99999999999 with any two digits in the decimal part or 100000000000 with only zeros in the decimal part:

^(?:(?:[0-9]|[1-9][0-9]{1,11})(?:.d{1,2})?|10{11}(?:.00?)?)$

See the regex demo.

  • ^ – start of string
  • (?: – start of a non-capturing group:
    • (?:[0-9]|[1-9][0-9]{1,11})0 to 99999999999 number
    • (?:.d{1,2})? – an optional sequence of a . char and then one or two digits
    • | – or
    • 10{11}(?:.00?)?1 and then 11 zeros optionally followed with . and one or two zeros
  • ) – end of the group
  • $ – end of string.
Advertisement