Tengo un formulario de contacto en HTML, que recopila informacion como nombre, email, fecha de ingreso y de salida. Esa información es enviada a mi email vía PHP. El problema está en que quiero validar algunos datos, como por ejemplo que:
- La cantidad de noches indicadas sean mayor a dos
- Que la fecha de salida sea mayor a la de entrada.... cosas básicas.
Para ello llamo a una rutina en JS muy sencilla que hace la verificación.
Hasta ahora todo bien, mi código HTML + JS + PHP, funciona en local host a la perfección, pero cuando subo los archivos a mi host remoto, en mi sitio web, pareciera que el script de JS es "salteado" y deriva directamente al archivo Php.
¿Alguien podría ayudarme?.... No logro que funcione cuando está publicado....
function validar(){
var name, email, pax, message, fechain, fechaout, expresion, noches, now, hoy;
name = document.getElementById("name").value;
email = document.getElementById("email").value;
pax = document.getElementById("pax").value;
fechain = document.getElementById("fechain").value;
fechaout = document.getElementById("fechaout").value;
message = document.getElementById("message").value;
expresion = /^[-\w.%+]{1,64}@(?:[A-Z0-9-]{1,63}\.){1,125}[A-Z]{2,63}$/i;
hoy = new Date();
now = hoy.getFullYear() + '-' + ( hoy.getMonth() + 1 ) + '-' + hoy.getDate();
//validación de fechas://
var fecha1 = moment(fechain);
var fecha2 = moment(fechaout);
noches = fecha2.diff(fecha1, 'days');
if(name === ""){
alert("Todos los campos son obligatorios");
return false;
}
else if(fechain > fechaout){
alert("Verifique la fecha de ingreso y de salida");
return false;
}
else if(noches < 2){
alert("El mínimo de noches es 2");
return false;
}
else if(noches > 15){
alert("Verifique la fecha de ingreso y de salida")
return false;
}
}
body {
/*background-image: url(/images/bosqueabedules.jpg);*/
/*background-size: 100vw 100vh;*/
background-attachment: fixed;
margin: 0;
font-family: 'Lato';
}
form {
width: 70%;
margin: auto;
/*background: rgba(0, 0, 0, 0.4);*/
padding: 9px;
box-sizing: border-box;
/*margin-top: 20px;*/
border-radius: 7px;
}
div {
}
h2 {
color: red;
text-align: center;
font-size: 30px;
margin-bottom: 20px;
}
input, textarea {
width: 100%;
margin-bottom: 20px;
padding: 7px;
box-sizing: border-box;
font-size: 17px;
border: none;
}
textarea {
min-height: 120px;
max-height: 300px;
max-width: 100%;
}
#boton {
background: forestgreen;
color: #fff;
padding: 20px;
}
#boton:hover {
cursor: pointer;
}
@media (max-width: 580px) {
form {
width: 95%;
}
}
label {
margin-bottom: 10px;
}
#boton_cancelar {
background-color: var(--bs-red);
padding: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Formulario de contacto responsive con HTML y CSS</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato&display=swap">
<link rel="stylesheet" href="assets/css/styles-form.css">
</head>
<body style="background: #d7c295;">
<section></section>
<div class="container" style="padding-left: 12px;">
<h1 class="text-start" style="font-size: 52.64px;font-family: Lato, sans-serif;">Contacto</h1>
<p class="text-start" style="font-size: 14px;">Por favor complete el siguiente formulario<br></p><html lang="es">
<head>
<title>Formulario de Contacto</title>
<meta charset="utf-8">
<link rel="stylesheet" href="assets/css/styles-form.css">
<script src="assets/js/validar.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
</head>
<body>
<form action="php/sendEmail.php" method="post" class="form-register" onsubmit="return validar();">
<input type="text" id="name" name="name" placeholder="Nombre">
<input type="email" id="email" name="email" placeholder="Correo electrónico">
<label>Fecha de ingreso:</label>
<input type="date" id="fechain" name="fechain" placeholder="Fecha de ingreso">
<label>Fecha de Salida:</label>
<input type="date" id="fechaout" name="fechout" placeholder="Fecha de salida">
<label>Pasajeros:</label>
<select id="pax" name="pax">
<option value="1">1</option>
<option value="2"selected>2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<textarea name="message" id="message" placeholder="Escriba su mensaje aqui!"></textarea>
<input type="submit" value="ENVIAR" name="submit" class="btn-enviar" ID="boton">
<input type="button" value="Cancelar" class="btn-enviar" ID="boton_cancelar">
</form>
</body>
</html>
</div>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/validar.js"></script>
</body>
</html>