Link to home
Start Free TrialLog in
Avatar of Stacey Fontenot
Stacey Fontenot

asked on

Validation of Input Field for Decimal Places

I have an input field that requires a user to enter time in a decimal form of hours + minutes. I need an html validation regex that will force a user to only enter the decimal 0 or 5. For example, user can enter the following valid numbers: (10.0; 10.5; 11.0; 3.5; 2.0) and so on. Invalid numbers:( 10.1; 10.2; 10.4; 3.2; 4.4;7.7). I just need to ensure user enters .0 or .5 decimal  no greater than 99.5, and great than 0.
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> ^([0-9][0-9]?).[0,5]$|^(99.5)$
That regex also matches the following:

99a5
The dot "matches anything except newline".  Thus, you can put a lot of things instead on the "a" (9995 also matches!).  So, if you want a literal dot, you need to escape it with a backslash OR enclose it in square brackets.

99,0
the characters in the square brackets are literal characters. So, "[0,5]" means "zero OR comma OR five".  So you need to either omit the comma or use the alternation operator => (0|5)

Try the following regex:
^(\d{1,2})\.(0|5)$

Open in new window

Avatar of Stacey Fontenot
Stacey Fontenot

ASKER

Excellent.