Copy link to clipboard
Copied
Hi
I am looking for some help with Javascript in relation to validation around birth dates.
I have a form and want to ensure that a user does not select the current years calendar from the Date Field when they select their birth date, the user will be older than 18 years preferably and I tried to use the following which I found on a website, but it comes up with an error.
var today = new Date();
var selectedDate = new Date(this.value);
if (selectedDate > today) {
app.alert("Please select a past date.");
return false;
}
Is someone able to assist?
Thanks Nicole
Copy link to clipboard
Copied
Enter the following custom validation script for a field that has a date format of dd/mm/yyyy:
function Scand(cFormat, cValue)
{
var oDate = util.scand(cFormat, cValue);
return new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate(), oDate.getHours(), oDate.getMinutes(), oDate.getSeconds(), 0);
}
var dob=event.value;
if(dob)
{
var oDob = Scand("dd/mm/yyyy" + " hh:mm:ss", dob + " 00:00:00") ;
var oToday = Scand("dd/mm/yyyy" + " hh:mm:ss", util.printd("dd/mm/yyyy", new Date()) + "00:00:00");
var age = oToday.getFullYear() - oDob.getFullYear();
if(util.printd("ddmm", oToday) < util.printd("ddmm", oDob) )
{age--;}
if(age<18)
{
app.alert("You must be older than 18.",1);
event.value="";
}
}
Copy link to clipboard
Copied
Try this:
var date=util.scand("mm/dd/yyyy", event.value);
if(date > new Date())
{
app.alert("Please select a past date.",1);
event.value="";
}
Note, this is for a field with a date format of "mm/dd/yyyy". The date format in the script must match the date format of the field.
Copy link to clipboard
Copied
Thank you I changed the date format to "dd/mm/yyyy" (Austalian) and that works great if someone tries to select a date in the future, however, if someone selects today's date (which can't be their date of birth) there is no error. The person MUST be an adult as the form is for staff so what would I would prefer that the date selected error out if someone selects a date that is not older than 18 years from todays date.
Are you able to help with that?
Copy link to clipboard
Copied
Enter the following custom validation script for a field that has a date format of dd/mm/yyyy:
function Scand(cFormat, cValue)
{
var oDate = util.scand(cFormat, cValue);
return new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate(), oDate.getHours(), oDate.getMinutes(), oDate.getSeconds(), 0);
}
var dob=event.value;
if(dob)
{
var oDob = Scand("dd/mm/yyyy" + " hh:mm:ss", dob + " 00:00:00") ;
var oToday = Scand("dd/mm/yyyy" + " hh:mm:ss", util.printd("dd/mm/yyyy", new Date()) + "00:00:00");
var age = oToday.getFullYear() - oDob.getFullYear();
if(util.printd("ddmm", oToday) < util.printd("ddmm", oDob) )
{age--;}
if(age<18)
{
app.alert("You must be older than 18.",1);
event.value="";
}
}
Copy link to clipboard
Copied
Thank you very much that works wonders.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more