Este fragmento presuponía un formulario HTML con una identificación de “formulario de edad” y tres entradas (texto o selección) con las identificaciones “día”, “mes” y “año”, respectivamente.
$("#age-form").submit(function(){
var day = $("#day").val();
var month = $("#month").val();
var year = $("#year").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);
var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
if ((currdate - mydate) < 0){
alert("Sorry, only persons over the age of " + age + " may enter this site");
return false;
}
return true;
});
Es posible que desee hacer algo más elegante que una alerta, y probablemente también debería validar el formulario con el código del lado del servidor o, de lo contrario, esta protección solo funciona para usuarios con JavaScript habilitado.