How to Create an Acrobat JavaScript Alert for Dates Before Today
Question details
The user needs an Acrobat JavaScript validation script to prevent users from entering a past date in a PDF form field named 'EffectiveDate'.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Validating user input in interactive PDF form fields to ensure the entered date is today or a future date.
- Observed behavior
- Comparing a raw string directly to a Date object fails. The script needs to accurately convert the string, compare timestamps, alert the user, clear the field, and reject invalid entries.
Ensure you have access to a PDF editor with advanced form creation and JavaScript editing capabilities. Verify the exact name of the date field (e.g., 'EffectiveDate') and its expected format (e.g., 'mm/dd/yyyy').
Implement a Custom JavaScript Validation Script
Use Acrobat JavaScript to convert the field's text string into a Date object, normalize it to midnight, and compare it with the current system date to validate the entry.
A common mistake when scripting date validations is comparing a string directly with a Date object. The input string must first be converted to a proper Date object using the util.scand() method.
Open your document in Form Preparation mode, right-click the 'EffectiveDate' text field, and select 'Properties'.
Navigate to the 'Validate' tab, select 'Run custom validation script', and click the 'Edit' button to open the code editor.
Initialize the current date by adding 'var d2 = new Date();' and normalize it to midnight using 'd2.setHours(0, 0, 0, 0);' to ensure accurate calendar day comparison.
Retrieve the field value using 'var sStart = this.getField("EffectiveDate").value;'. If it has length, convert it securely by adding 'var dateStart = util.scand("mm/dd/yyyy", sStart);' and normalize it with 'dateStart.setHours(0, 0, 0, 0);'.
Write an if statement comparing the timestamps: 'if (dateStart.getTime() < d2.getTime())'. Inside the block, trigger an alert using 'app.alert()', clear the invalid entry with 'event.value = "";', and reject it by setting 'event.rc = false;'.

Manage and Fill PDF Forms Easily with WPS Office
WPS Office includes a comprehensive PDF editor that allows you to open, fill, and manage interactive PDF forms effortlessly. Whether you are validating documents or inputting data, WPS offers a fast and user-friendly experience.
- 1. Launch WPS Office: Open WPS Office on your computer and navigate to the 'PDF' tab on the main dashboard.
- 2. Open Your PDF Form: Click 'Open' to browse your local files and select the interactive PDF document you need to fill or review.
- 3. Fill and Save: Click directly on the interactive form fields to input your data. Once completed, click 'Save' to preserve your inputs securely.

Frequently Asked Questions
Why does my Acrobat JavaScript date comparison fail?
If you compare a text string directly with a Date object (e.g., if (sStart < d2)), the script will evaluate incorrectly. You must parse the input string into a Date object using the util.scand() method before comparing.
How do I ignore the time of day when comparing dates in JavaScript?
You can normalize both Date objects to midnight by applying the setHours(0, 0, 0, 0) method to them. This strips the time component, ensuring only the calendar dates are evaluated.
How do I clear the form field if the validation script fails?
Within your custom validation script's conditional block, set event.value = ""; to instantly clear the user's entry, and set event.rc = false; to completely reject the invalid input.




