Home > Blockchain >  Regex - Validation of numeric with up to 10 decimal places (0.01 - 100.0000000000)
Regex - Validation of numeric with up to 10 decimal places (0.01 - 100.0000000000)

Time:02-11

I am just curious if this regex expression will be able to be shorten. it should allow ten digits.

/^(?!0 (?:\.0 )?$)(\d{1,2}\.\d{1,10}|\d{1,2}|(100)|(100\\.00)|(100\\.0000000000)|(100\\.000000000)|(100\\.00000000)|(100\\.0000000)|(100\\.000000)|(100\\.00000)|(100\\.0000)|(100\\.0000)|(100\\.000)|(100\\.0))$/

CodePudding user response:

You need to use

/^(?![0.] $)(?:\d{1,2}(?:\.\d{1,10})?|100(?:\.0{1,10})?)$/

See the regex demo.

Details:

  • ^ - start of string
  • (?![0.] $) - no only 0 and/or . chars till end of string are allowed
  • (?: - either of
    • \d{1,2}(?:\.\d{1,10})? - one or two digits and then an optional occurrence of a . and one to ten digits
  • | - or
    • 100(?:\.0{1,10})? - 100 and then an optional occurrence of a . and one to ten 0 chars
  • ) - end of the group
  • $ - end of string.
  • Related