Validation of date by JQuery

1

Hello good morning I have the next function that validates dates without the "/" example 010218 that would be 01/02/2018 , also if only 4 characters come I add it through php the year. Obviously the date armo by .substr . The help I need basically would be to restrict tomorrow. For example, if today is 13/04/2018 (130418) that a date higher than that day can not be added. Example the next day (140418) . Thank you very much!

function ValidarF() {

var date1Var = $("#Fecha").val();
var date1;
var mes = date1Var.toString().substr(2,2);       
var dias = date1Var.toString().substr(0,2);
var anio = date1Var.toString().substr(4,2);
 //Si vienen 3 caracteres tiro error.
if(date1Var.length==3)
{
swal("","Fecha incorrecta","warning"); 
}
 //Hago lo mismo con 5.
if(date1Var.length==5)
{
swal("","Fecha incorrecta","warning"); 
}
if(date1Var.length==4)
{
//En este caso valido que el mes sea el rango de 1 a 12 y dias de 1 a 31
if (mes<1 ||mes>12 || dias<1 ||dias>31)
{
swal("","Fecha incorrecta","warning");   
}
}

if(date1Var.length==6)
{ 

if (mes<1 || mes>12 || dias<1 ||dias>31)
{
swal("","Fecha incorrecta","warning");  
}

}
//Seteo el año hasta 2018
if (date1='20'+anio>2018)
{
swal("","Fecha incorrecta","warning");  
}

}

PHP:

$date1Var = $_REQUEST["Fecha"];
if(strlen($date1Var)==4) {
$date1=date("Y").'-'.substr($date1Var,2,2).'-'.substr($date1Var,0,2);

} elseif(strlen($date1Var)==6) {
$date1='20'.substr($date1Var,4,2).'-'.substr($date1Var,2,2).'-'.substr($date1Var,0,2);
} else { 
$date1 = date('Y-m-d');
}

    
asked by Nacho Carpintero 13.04.2018 в 12:32
source

2 answers

2

After all the validations, your date should have the format dias-mes-anio . You can build a Date object by passing the string in anio-mes-dias format, and check that the timestamp of that object is not greater than today.

var fecha_bonita='01-04-2018';
var fecha_objeto=new Date('2018-04-01');
if(fecha_objeto.getTime() > Date.now() ) {
  console.warn('Fecha no puede ser mayor a hoy');
}

function ValidarF() {

var validada=jQuery('#validada');
var date1Var = $("#fecha").val();
var date1;
var mes = date1Var.toString().substr(2,2);       
var dias = date1Var.toString().substr(0,2);
var anio = date1Var.toString().substr(4,2);
 //Si vienen 3 caracteres tiro error.
if(date1Var.length!==4 && date1Var.length!==6) {
  validada.text("Fecha debe contener 4 o 6 dígitos"); 
  return;
}

if(date1Var.length==4) {
  $("#fecha").val(date1Var+'18');
  anio='18'
}

if (mes<1 || mes>12 || dias<1 ||dias>31)  {
    validada.text("Mes debe ser entre 01 y 12, dias entre 01 y 31");  
    return;
}

anio='20'+anio;
//Seteo el año hasta 2018
if (anio>2018) {
  validada.text("Año no puede ser mayor a 2018");  
  return;
}
var fecha_formateada=[dias,mes,anio].join('-');
var fecha_iso = [anio,mes,dias].join('-');
var date=new Date(fecha_iso);

if(date.getTime()>Date.now()) {
  validada.text("Fecha no puede ser mayor a hoy");  
  return;
}
validada.text(fecha_formateada);

}

$(document).on('click','#validar',function() {
  ValidarF();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="validar">Validar Fecha</button>
<br>
<div>
Ingrese fecha:
<input type="text" id="fecha"/>
</div>
<br>
<div>
Fecha validada:
<span id="validada"></div>
</div>
    
answered by 13.04.2018 / 13:15
source
2

Here is the validation part for the next day, I have changed the substring of the date to be able to adapt it to the object Date of Javascript.

var date1Var = "13/04/2018";

var dias = date1Var.toString().substr(0,2); // 13
var mes = date1Var.toString().substr(3,2); // 04      
var anio = date1Var.toString().substr(6,4); // 2018

var fecha = new Date(anio, mes, dias, 0, 0, 0, 0); // 13/04/2018
var fechaDiaSiguiente = new Date(anio, mes, dias + 1, 0, 0, 0, 0); /1404/2018

if(fechaDiaSiguiente > fecha) {
  /// La fecha es mayor por lo que aqui haces algo
  swal("","Fecha incorrecta","warning"); 
}
    
answered by 13.04.2018 в 12:55