I see some problems in your code. First the name of the function called in onblur
is different from the function name:
@onblur = "return ValidaHora(this)" <-- "V" maiúsculo
↑
function validaHora(x){ <-- "v" minúsculo
↑
If it was not a typo of the question, only then would it make an error of non-existent function and the code wouldn’t work.
But if it was just a typo of the question and the names are the same in the code, it would give error in the x.value.length(5)
: the .length
is not a function, therefore has no ()
after, it would just be x.value.length
.
Another problem is the use of alert
with .focus()
, because this will catch the page by firing the alert
and endlessly, because when the if
is satisfied, the field will gain focus and by clicking to close the alert
, the field will lose focus by calling again the function and entering again the if
, creating an infinite loop where the alert box will be displayed continuously, locking the tab.
One way to avoid this, in addition to the aforementioned corrections, is to create a flag (control variable) to prevent the alert
is fired again when closing it:
var alerta; // flag
function ValidaHora(x){
if (x.value.length < 5){
if(!alerta){
alerta = true;
alert("ERRO!");
setTimeout(function(){
x.focus();
alerta = false;
}, 1);
}
return false;
}else{
alert("OK!");
return true;
}
}
Mínimo 5 caracteres:<br>
<input onblur="return ValidaHora(this)">
What is the criterion of "fully fulfilling"?
– Sam