Amateur
Amateur

Reputation: 75

Can we validate Min and Max Value for a floating Number using RegExp?

Hi I know that we can validate Min and Max Length of a number using Regex. But can we validate Min and Max Value for a floating point number using the same?

Could anyone please just apply Min and Max Value to following Regex:

^(?=.*\d)(?!.*?\.[^.\n]*,)\d*(,\d*,?)*(\.\d*)?$

Above Regex matches a floating number with optional decimal point and commas.

Upvotes: 3

Views: 29916

Answers (2)

Kannan Mohan
Kannan Mohan

Reputation: 1850

I guess this should help you. This regex will match 0.00 to 100,000,000.00 upto 2 decimal places.

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

But keep in mind that its always best to compare numbers numerically that using regex.

Here is the link to verify it.

Upvotes: 2

Lorenz Meyer
Lorenz Meyer

Reputation: 19915

Regex is for strings. You try to compare floats. It's just not the right tool. It's worse than eating your soup with a fork. It's like writing on paper with a knife or cutting your hair with a teaspoon.

Look here for a solution with positive integers without thousands separator : Using regular expressions to compare numbers

I leave the task to you to extend that solution to using floats, thousands separator and negative numbers.

Upvotes: 12

Related Questions