Skip to main content
Participant
June 10, 2025
Question

RE: PDF Form and Date Validation

  • June 10, 2025
  • 2 replies
  • 522 views

Hello,

 

I am currently processing a Date control/field on a PDF form, and I want to test if a value (entered via the Date Control field/ or manually typed) is NOT the format of dd/mm/yyyy and or is greater than Today's date, how can I test for that?

 

Below is the custom validation script I have written so far...

 

The 'greater than' part works - if (formatDate.setHours(0,0,0,0) >= todaysDate.setHours(0,0,0,0)), but how can I test for a value that is entered as mm/dd/yyyy or just a text value such as 'Test'. Both of these entries would be incorrect. I have tried with - else if (dateParse <= 0), and that does not work. 

 

// Current Date Field
if (event.value != ""){
var todaysDate = new Date();
var formatDate = util.scand("dd/mm/yyyy", event.value);
var dateParse = Date.parse(event.value);
if (formatDate.setHours(0,0,0,0) >= todaysDate.setHours(0,0,0,0)) {
app.alert("Invalid date format entered -- please enter in DD/MM/YYYY format, \n\nfor example, 18/05/1999)", 2, 1, " Please check date entered");
event.value = "";
} else if (dateParse <= 0) {
app.alert("Invalid date entered -- please enter in DD/MM/YYYY format, \n\nfor example, 18/05/1999)", 2, 1, " Please check date entered");
event.value = "";
}
}

2 replies

PDF Automation Station
Community Expert
Community Expert
June 10, 2025

Enter the following custom validation script:

if (event.value) 
{
if (util.scand("mm/dd/yyyy", event.value)==null) 
{
app.alert('Invalid date value.  Please use the format "mm/dd/yyyy".');
event.value="";
}
else
{
if(util.scand("mm/dd/yyyy", event.value)>= util.scand("mm/dd/yyyy", new Date()))
{
app.alert('Invalid date value.  Please enter a date before tomorrow\'s date.');
event.value="";
}
}
}
Participant
June 11, 2025

Hello,

 

I tried the following...

 

// Current Date Field
if (event.value != ""){
var todaysDate = new Date();
var formatDate = util.scand("dd/mm/yyyy", event.value);
if (formatDate == null) {
app.alert("Invalid date entered -- please enter in DD/MM/YYYY format, \n\nfor example, 18/05/1999)", 2, 1, " Please check date entered");
event.value = "";
} else
{
if (formatDate.setHours(0,0,0,0) <= todaysDate.setHours(0,0,0,0)) {
app.alert("Invalid date format entered -- please enter in DD/MM/YYYY format, and which is greater than today's date. \n\nfor example, 10/04/2030)", 2, 1, " Please check date entered");
event.value = "";
}
}
}

 

NOTE: 

var formatDate = util.scand("dd/mm/yyyy", event.value);
if (formatDate == null) 

 

Unfortunately, this does not work when the values of 'Test' or '12/45/1999' are entered. 

PDF Automation Station
Community Expert
Community Expert
June 11, 2025

First, I gave you the script that works so why are you changing it and telling me yours doesn't work again?  Next, in your original question you said to test "is greater than Today's date" but you seem to be trying to test whether the date is less than or equal to today's date, which is it?  Is > than today's date a pass or a fail?  Also, a field formatted as a date has its own error for typing the word test so the alert in the script that tests for improper formats will never display.

PDF Automation Station
Community Expert
Community Expert
June 10, 2025

By "Date control/field" do you mean a field formatted as a date?  By "entered via the Date Control field" do you mean selected with the popup calendar?

Participant
June 10, 2025

Hello,

 

It is a Date Field (added from the Form Component Menu).

 

So with the Popup Calendar Control (you can TYPE a value such as 'Test' or '12/31/1989' and would need to be defined as invalid. I need to know HOW to test/validate for this!

 

With the Control, if I select a date that is greater than today, if works with validation: if (formatDate.setHours(0,0,0,0) >= todaysDate.setHours(0,0,0,0)).

Thom Parker
Community Expert
Community Expert
June 10, 2025

So a date field is just a normal text field that uses the built-in Acrobat data entry and formatting scripts. This means that the user is prevented from entering a date that does not match the specified format. So that part of your ask is already handled. 

But if you really wanted to test the entered format, a Regular Expression would do a better job.  

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often