8
I am trying to validate a time field in PHP, and with the help of this topic on an external site I came to 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
must receive the field in format '00:00', but has no validation in JS, and the user can enter an invalid time.
So what I want is 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 the 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
Very good! Thanks a lot!
– gustavox