Condiciones para campos tipo Fecha

Tabla de contenidos

Athento permite agregar condiciones para que no se permita seleccionar una fecha posterior o anterior a la presente. 

Para esto debes tener en cuenta lo siguiente. 

Javascript para no permitir ingresar una fecha anterior a la presente

$('.metadata_name').change(function() {
var userDate = $('.metadata_name').first().val();
var today = new Date();
today.setHours(0,0,0,0);
const [day, month, year] = userDate.split('-');
var selectedDate = new Date(year, month-1, day);
console.log(selectedDate)
console.log(today)
if (selectedDate < today) {
toastr.error("La fecha no puede ser menor a hoy");
let dia = today.getDate();
let mes = today.getMonth() + 1;
let ano = today.getFullYear();
let fechaFormateada = dia + "-" + mes + "-" + ano;
$('.metadata_name').val(fechaFormateada);
console.log(fechaFormateada)
return false;
}
return true;
})

Javascript para no permitir ingresar una fecha futura a la presente

$('.metadata_name').change(function() {
var userDate = $('.metadata_name').first().val();
userDate = userDate.slice(0,10);
console.log(userDate)
const [year, month, day] = userDate.split('-');
var selectedDate = new Date(year, month-1, day);
var today = new Date();
today.setHours(0,0,0,0);
console.log(selectedDate)
console.log(today)
if (selectedDate > today) {
toastr.error("La fecha no puede ser mayor a hoy");
let dia = today.getDate();
let mes = today.getMonth() + 1;
let ano = today.getFullYear();
let fechaFormateada = dia + "-" + mes + "-" + ano;
$('.metadata_name').val(fechaFormateada);
console.log(fechaFormateada)
return false;
}
return true;
})

Ahora bien, si tenemos un campo tipo tabla con una columna fecha, se debe tener en cuenta el siguiente Javascript.

function restringir(e, data, action, item) {
if ( action !== 'remove' ) {

var subtotal = item.field( 'column name' ).val();

//userDate = subtotal.slice(0,10);
console.log(typeof subtotal)
const [day, month, year] = subtotal.split('/');
var selectedDate = new Date(year, month-1, day);
var today = new Date();
today.setHours(0,0,0,0);
console.log(selectedDate)
console.log(today)
if (selectedDate > today) {
toastr.error("La fecha no puede ser mayor a hoy");
return false;
} else if ( item.inError() ){
return false;
}
return true;
}
}
window.presubmit_row_metadata_name = restringir;