MauMau 254
MauMau 254

Reputation: 1

Regular expression Range with decimal validation

I am trying to set the minimal amount that can be donated on a donation form to be $5.00. The field property allows me to validate using a regular expression. The problem with my expression is that it allows users to donate amounts less than $5.00 if they use decimals. eg $1.99 or $2.50 or $3.75. I need help coming up with a regular expression that will restrict users from using decimal points. Any help will be greatly appreciated.

Thanks

Here is what I have ...

/([5-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-8][0-9]{4}|9[0-8][0-9]{3}|99[0-8][0-9]{2}|999[0-8][0-9]|9999[0-9]|[1-8][0-9]{5}|9[0-8][0-9]{4}|99[0-8][0-9]{3}|999[0-8][0-9]{2}|9999[0-8][0-9]|99999[0-9]|[1-8][0-9]{6}|9[0-8][0-9]{5}|99[0-8][0-9]{4}|999[0-8][0-9]{3}|9999[0-8][0-9]{2}|99999[0-8][0-9]|999999[0-9]|10000000)/

Upvotes: 0

Views: 93

Answers (1)

Vincent
Vincent

Reputation: 4753

Here you go:

^0*([5-9]|[1-9]\d{1,})(\.\d{1,2})?$

Test link : https://regex101.com/r/p65pXd/4

Explanation:

^0* means any number of leading zeros

[5-9] is a single number between 5 to 9 (both included)

[1-9]\d{1,} is a number greater or equal to 10

Then we put both of the previous regexs into ( | ) to have a 'OR' condition to match one of those

Then we can add (\.\d{1,2})?, means "optionally followed by a dot and 1 or 2 numbers."

We can surround this ^ and $ meaning beginning and end of input.

Upvotes: 1

Related Questions