4
The next form
:
<form id="form_id" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" autocomplete="off" onsubmit="valida_form()">
I have a function that checks for blank spaces:
function valida_form(evt)
{ Array.from(document.getElementById("form_id")).forEach(function(element,index)
{ if(element.value === "")
{ form_err++;
}
});
if(form_err > 0)
{ alert("Há erros:"+form_err);
evt.preventDefault();
}else
{ alert("Sem erro"+form_err);
}
}
meanwhile the form
continues to be sent.
I’m learning the passage of arguments, and callbacks
then if the error is something related, I appreciate the explanation.
I think we’re missing the scope here
valida_form(this)
andalert
is blocking, could prevent his default event or exchange forconsole
– BrTkCa
if it says on the form tag, if it is I tried..?
– MagicHat
That’s right @Magichat,
onsubmit="valida_form(this)"
– BrTkCa
So I tried this tmb... it alerts the error and Talz, but sends the form,,
– MagicHat
for testing, put the
evt.preventDefault()
in the first line of the function body– BrTkCa
following him is not recognizing the element of
preventDefaut
...he says:evt.preventDefault is not a function
...this leaving the method where it is equal in the question example– MagicHat
For some reason in the tag it does not "recognize" the
preventDefault
, maybe because it is a function invoked or because it processes Submit differently, this is how it works, see: https://jsfiddle.net/lbclucascosta/xvf80gy0/– BrTkCa