Dman100
Dman100

Reputation: 747

check for minimum amount in regular expression

I have a regular expression that checks for a value between the range of 0 and 999999.

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

The problem is that 0.00 is valid. I would like to set 0.01 as the smallest amount that is valid. So, putting in 0.00 would me invalid.

I'm terrible with the black art of regex. Can anyone help?

Thanks.

Upvotes: 1

Views: 216

Answers (1)

Andy Lester
Andy Lester

Reputation: 93805

Don't put logic into regular expressions. They are for matching patterns, not doing numerical comparisons.

In Perl, this would be:

if ( $s =~ /^((?:\d{1,3},)?\d{1,3})(\.\d{2})$/ && ( $s > 0 ) ) {
    # acceptable number
}

Regexes aren't a black art if you use them properly. Trying to do numeric calculations with them is not using them properly.

Upvotes: 5

Related Questions