I want to validate a time with a regex. I created the following expression :
'#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#'
Here is the problem:
<?php
var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:25'));
// Returns 1 (OK)
var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '25:25'));
// Returns 0 (OK)
var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:2555'));
// Returns 1 (instead of 0 as I would like to get)
?>
Does anybody know what is wrong?
Time in 24-Hour format regular expression pattern :
The 24-hour clock format is start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59 then (optionally a semi colon (:) and follow by 00-59).
Description :
Matching time formats :
Not matching time formats :
Example :