Question

i searching a way to write a regular expression for validate numbers that are multiples of 0.05

I need to validate number that have at least two decimal digit and if there is a second digit after the decimal point this digit need to be 5.

How can i modify this:

/^\d+(\.\d{0,2})?$/

Thanks for any type of help

Was it helpful?

Solution 2

This'll do it for you:

/^\d+(?:\.\d)?[05]?$/

The nice thing about this regex is that it will also allow for rationals with only a tenths digit

var re = /^\d+(?:\.\d)?[05]?$/;

re.test(1.77)
=> false
re.test(1)
=> true
re.test(1.05)
=> true
re.test(1.07)
=> false
re.test(1.1)
=> true
re.test(1.10)
=> true
re.test(.5)
=> true
re.test(123.5)
=> true

OTHER TIPS

\d+\.\d[05]

That should work. This matches any digit, followed by any digit, followed by a 0 or 5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top