Jbisgood9999999 Jbisgood9999999 - 4 months ago 13
Javascript Question

Regular Expression to accept only positive number and decimals (only end with .5 .0 or integer accepted)

I need a regular expression in javascript that will accept only positive numbers and decimals end with .5 or .0 .

(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)


This is what i have but i don't know how to change to reject 0 and 0.0, and reject those end with .1 .2 .3 .4 .6 .7 .8 .9

Accept:
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4


But

Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
0
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01

Answer

You can check this here at regex101. The regex I used to do this is:

^(0\.5)$|(^[1-9][0-9]*(\.[05])?)$

What is highlighted in blue is a match.

Comments