I have a very simple form validation, but it doesn't work and I can't see why. When I press submit, it goes to the header of the page instead of displaying the paragraph with the message.
document.getElementById("warning").style.display = 'none';
function validacion() {
let nombre = document.getElementById("campoNombre").value;
let email = document.getElementById("campoEmail").value;
if (nombre === null || email === null || nombre.length === 0 || email.length === 0) {
document.getElementById("warning").style.display = 'block';
return false;
}
};
<div style="text-align:center;">
<h3>Solicitar información</h3>
<form method="POST" id="formulario" onsubmit="return validacion();">
Nombre:<br><input type="text" name="name" id="campoNombre"><br>
Email:<br><input type="text" name="mail" id="campoEmail"><br><br>
<input type="submit" value="Solicitar información" name="submit" id="submit">
</form>
</div>
<div>
<p id="warning">No se enviaron los datos</p>
</div>
Someone can give me a cable?