I believe its purpose was to check the field before the form was sent.
Note that you are working with events. Events relate to who fired it.
In this case the correct use of the onkeyup
input, as it is triggering the event each time "a tile comes back" from an insert.
But he has nothing to do with sending form
for this it is necessary to use another event the onsubmit
.
Remembering that it relates to the element that fired it, in this way it would not be possible to use the input event in the form, because the this
should be the one of input
and not of form
.
In your case just 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>
'Cause you don’t wear any tris
but a search for element.
Dude, when you want to access the function by submitting a form, put onsubmit and not onClick. Because in onClick it performs the function, but will reload the screen, got it? One more thing, the title of your question is very vague. Try to specify more your doubt.
– Pedro Morais