php - How to validate time string in hh:mm using preg match? -
below following possiblities. time 12 hours
inputs output should "05:30" true "asasds" false "05:30:sads" false "adas:05:40" false "04:30:40" false below code wrote,
$value = "05:30" ; if(!preg_match("/(1[012]|0[0-9]):([0-5][0-9])/", $value)){ echo "failed"; exit; } echo "passed"; exit; but prints passed if give $value = "05:30:asdsa". need output "failed".
use anchors match start , end, e.g.
!preg_match("/^(1[012]|0[0-9]):([0-5][0-9])$/", $value) //^ see here ^
Comments
Post a Comment