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

RE: PDF Form and Date Validation

New Here ,
Jun 10, 2025 Jun 10, 2025

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

TOPICS
How to , JavaScript , PDF forms
184
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 ,
Jun 10, 2025 Jun 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?

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 ,
Jun 10, 2025 Jun 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)).

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 ,
Jun 10, 2025 Jun 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Jun 11, 2025 Jun 11, 2025

Hello Thom,

 

I appreciate the response.

 

I can see that built-in validation for dates is excellent, but I would like to edit the pop-up box messages with my content. How can I do this?

 

Regarding using a Regular Expression, would the following prevail, in testing for the format dd/mm/yyyy and not accepting invalid values such as 'Test' or '12/31/2000' or '11.34.65' ?

 

var valueFromDateControl = event.value

var matches = valueFromDateControl.match(/^\d?\d\/(\d?\d)\/\d{4}$/);

 

Your assistance is much appreciated.

 

Cheers

 

Jason

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 ,
Jun 10, 2025 Jun 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="";
}
}
}
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 ,
Jun 11, 2025 Jun 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. 

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 ,
Jun 11, 2025 Jun 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.

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 ,
Jun 11, 2025 Jun 11, 2025

Hello,

 

I appreciate your response, but do stop with the aggressive tone. It is not needed.

 

My mistake first, it is greater than I want, but I am testing for less than as well. I copied the wrong code there

 

OK, fair point, I will test with your direct code, but the format required is dd/mm/yyyy, not mm/dd/yyyy.

 

I will say thank you, regards the 'Test' value validation update.

 

If you are going to reply, please keep it civil.

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 ,
Jun 11, 2025 Jun 11, 2025

I asked you questions.  That's all.  When someone takes the time to provide a script you should at least test it before posting your own in a reply and saying "does not work".  That puts it back on me to take even more time and troubleshoot your script.  Your script works for me, although as previously stated, your incorrect format alert will never pop when the field is formatted for a date because an format error is already programmed in to date formats.

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 ,
Jun 11, 2025 Jun 11, 2025
You gave a working script, yet it was altered unnecessarily and reintroduced with issues. Clarify your intent: should the date be greater than today's for a pass, or is that a fail? Also, date fields auto-block invalid formats, making your alert for improper format redundant. 
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 ,
Jun 11, 2025 Jun 11, 2025
LATEST
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