I believe your intent was to check the field before the form is submitted.
Note that you are working with events . Events relate to who shot you.
In this case it is correct to use onkeyup
in the input, since it is firing the event every time a tile "returns" from an insert.
But it has nothing to do with sending form
so it is necessary to use another event onsubmit
.
Remembering that it relates to the element that triggered it, this way it would not be possible to use the input event in the form, since the this
should be input
and not form
.
In this case you just need to do:
function _celular(){
var celular = document.getElementById("celular").value;
var ExpReg = /^[0-9]{11}$/;
if (ExpReg.test(celular)) {
alert("Sim passou no teste.");
return true;
}
alert("Não passou no teste.");
return false;
}
<form onsubmit="_celular()">
<input type="text" id="celular" onkeyup="_celular()">
<input type="submit" value="enviar">
</form>
You do not use any tris
, but rather a search for element.