Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PDF Form: Validation for birth dates

New Here ,
Feb 18, 2025 Feb 18, 2025

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

 

TOPICS
PDF forms
817
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 20, 2025 Feb 20, 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="";
}

}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2025 Feb 18, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 20, 2025 Feb 20, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2025 Feb 20, 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="";
}

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 20, 2025 Feb 20, 2025
LATEST

Thank you very much that works wonders.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines