I need a regex which restricts an hour entry from 0-24 with the fraction in multiples of .25. Please keep in mind the following conditions must be met:
- When whole part is 0-23, the fractional part can be .0, .25, .5, or .75
- When whole part is 24, the fractional part can be .0
- When no whole part is present, the fractional part must be .0, .25, .5, or .75
- Empty string is not allowed
I am very confident that the following expression can be tightened up, but this is what is in place thus far:
(^(([0-9])|(1[0-9])|(2[0-3]))(\.((0*)|(250*)|(50*)|(750*)))?$)|(^24(\.0*)?$)|(^\.((0+)|(250*)|(50*)|(750*))$)
Update: The regex validation exists both on the client (javascript) and server (asp.net, c#).
And the reason why you absolutely, positively need to do this as a regular expression is why? Converting to a number and doing a few basic checks would be far easier.
Check if the number multiplied by 4 is a natural number <= 96, done.
Judging by your profile you’re probably using .NET. I assume you want this as a regular expression because you’re using a it for client side validation. I would seriously consider using a CustomValidator with a client side JavaScript.
Something along these lines for the JavaScript:
Edit: I was slightly concerned about rounding errors when using floats, but it seems to work out okay for all possible cases.