Hi I want to validate a string to check f it has valid time format. HH:MM
However,(due to some insane business reasons) I also want to validate 24:00
as correct.
The following works {([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]}
but does not validate 24:00
.
Hi I want to validate a string to check f it has valid time format. HH:MM
However,(due to some insane business reasons) I also want to validate 24:00
as correct.
The following works {([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]}
but does not validate 24:00
.
-
it means that
00:00
would be an invalid time? – zzlalani Commented Feb 3, 2014 at 7:20 -
1
@zzlalani - Some panies uses a 30-hour system
00:00
to29:59
:) – Derek 朕會功夫 Commented Feb 3, 2014 at 7:21 - @Derek朕會功夫.. so they are using 25-hour and 1 second? – zzlalani Commented Feb 3, 2014 at 7:22
4 Answers
Reset to default 1Just change 2[0-3]
to 2[0-4]
:
^([0[0-9]|1[0-9]|2[0-4]):[0-5][0-9]$
Or if you only want to include 24:00
:
^((([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9])|(24:00))$
You can use:
// Allows times like 24:05:00
function validateTime(s) {
var t = s.split(':');
return /^\d\d:\d\d:\d\d$/.test(s) &&
t[0] >= 0 && t[0] < 25 &&
t[1] >= 0 && t[1] < 60 &&
t[2] >= 0 && t[2] < 60;
}
Try this,
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
Try this Regex:
/^(?:0\d|1[01]):[0-5]\d$/