I'm trying to validate a time field in PHP, and with the help this topic on an external site I came across this script:
function validaHoras($campo){
if (preg_match('/^[0-9]{2}:[0-9]{2}$/', $campo)) {
$horas = substr($campo, 0, 2);
$minutos = substr($campo, 3, 2);
if (($horas > "23") OR ($minutos > "59")) {
$campo = false;
}
}
}
The input
should receive the field in the format '00: 00 ', but it has no validation in JS, and the user may enter an invalid time.
So I want to change the value of the variable to false
when it comes with a wrong time (type 99:99), but it is not working. Testing the script below with input
99:99, the value of the variable is not changed ...
function validaHoras($campo){
if (preg_match('/^[0-9]{2}:[0-9]{2}$/', $campo)) {
$horas = substr($campo, 0, 2);
$minutos = substr($campo, 3, 2);
if (($horas > "23") OR ($minutos > "59")) {
$campo = false;
}
}
}
// SEGUNDA
$val1 = isset($_POST["Tsegs"]) && !empty($_POST["Tsegs"]) ? $_POST["Tsegs"] : false;
validaHoras($val1);
echo $val1;
//saida: 99:99
//saida desejada: false