Mayank
Mayank

Reputation: 1392

Regex For digit count in an alphanumeric string

How can i validate following logic from regex

Allowed string Example:

Does not allow : - $999999999.99 ( 9 digit before decimal) - $99,99,999,99.99

Mean i want to restrict the count of digit only before decimal. How can i achieve this. Thanks in Advance

Upvotes: 3

Views: 3928

Answers (2)

Vladu Ionut
Vladu Ionut

Reputation: 8193

/^(\$?(\,?\d){1,8}\.\d{2}$)/gm

Regex Demo

Upvotes: 3

anubhava
anubhava

Reputation: 785058

You can use this regex:

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

RegEx Demo

Explanation:

^              # Line start
\$?            # match optional $ at start
(?:,?\d)       # Match an optional comma followed by a digit and use non-capturing group
{1,8}          # up to 8 occurrence of previous group
(?:\.\d{1,2})? # followed by optional decimal point and 1 or 2 digits
$              # line end

Upvotes: 4

Related Questions