Skip to main content
Participant
February 19, 2025
Answered

PDF Form: Validation for birth dates

  • February 19, 2025
  • 1 reply
  • 649 views

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

 

Correct answer PDF Automation Station

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="";
}

}

1 reply

PDF Automation Station
Community Expert
Community Expert
February 19, 2025

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.

Participant
February 21, 2025

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?

PDF Automation Station
Community Expert
Community Expert
February 21, 2025

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="";
}

}