How to Use JavaScript for Conditional PDF Fields Based on a Date
Question details
The user needs to use JavaScript to automatically copy values into dependent form fields when a date field is populated, and clear them when the date field is empty.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where specific text fields rely on a date field's input status to display data from previous entries.
- Observed behavior
- The dependent fields must mirror previous entries (like EL1, EG1, ED1) when the date field (DATE2) is filled, but automatically clear themselves if the user leaves DATE2 blank.
Ensure that your PDF form is open in editing mode and that all text fields involved (such as DATE2, SL2, EL1) have been created and correctly named before applying the script.
Apply a Custom Validation Script to the Date Field
Use Acrobat's built-in JavaScript validation properties to trigger conditional updates on dependent fields whenever the date field's value changes.
By binding a script to the custom validation event of the date field, you ensure that dependent fields react in real-time. The script uses a simple conditional operator to check if the date field is empty, clearing the target fields if true, or populating them with source values if false.
Enter 'Prepare Form' mode in your PDF editor. Right-click your target date field (e.g., DATE2) and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Validate' tab at the top.
Select the 'Run custom validation script' radio button, then click the 'Edit' button next to it.
Paste the following code into the JavaScript Editor: this.getField("SL2").value = event.value == "" ? "" : this.getField("EL1").value; this.getField("SG2").value = event.value == "" ? "" : this.getField("EG1").value; this.getField("SD2").value = event.value == "" ? "" : this.getField("ED1").value;
Click 'OK' to close the editor, then 'Close' the Properties window. Exit form editing mode to test the form by entering and deleting dates in DATE2 to see the fields update automatically.
Easily Create and Manage Interactive PDF Forms with WPS Office
While advanced JavaScript is useful for complex forms, many interactive PDF forms can be built quickly and easily using standard tools. WPS Office provides an intuitive, user-friendly interface for creating, filling, and managing PDF forms without requiring deep programming knowledge.
- 1. Open Your PDF: Launch WPS Office and open your PDF document using the dedicated PDF tab.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to reveal interactive field options.
- 3. Insert Interactive Fields: Click on the text field, checkbox, or date field icons, then drag onto the page to place them exactly where you need.
- 4. Customize and Save: Double-click the inserted fields to configure their names and basic properties, then save the document for distribution.

Frequently Asked Questions
Why isn't the JavaScript executing when I test my PDF form?
Many basic PDF readers and web browsers disable JavaScript for security reasons. To ensure scripts run properly, test and view your form using a dedicated, fully-featured PDF software application with JavaScript execution enabled in its preferences.
Can I use this validation script for regular text fields instead of dates?
Yes. The script checks if the current field is empty (event.value == ""). You can apply this exact logic to standard text boxes, dropdowns, or combo boxes to control conditional formatting.
How do I find the exact field names to reference in my script?
In your PDF form editor, double-click a form field to open its properties. The exact, case-sensitive name of the field will be displayed in the 'Name' box under the 'General' tab.




