How to Set Conditional Date Fields in PDF Forms Using JavaScript
Question details
The user wants to dynamically calculate a dispatch date (like Order Date + 1) in a PDF form based on specific conditions, while ensuring the output field remains manually editable.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms where date values need to auto-calculate only when certain related item fields are filled out by the user.
- Observed behavior
- When using standard calculation scripts, the target date field becomes read-only and cannot be manually overridden. The user needs a method to trigger dynamic date calculation without locking the field.
Ensure you are using a professional PDF editor that supports JavaScript execution and form field properties, and verify that your PDF contains named text fields for the order date, article, and dispatch date.
Use JavaScript in the Field Validation Event
Move the JavaScript code from the Calculation tab to the Validation event of the trigger field. This ensures the script only runs upon data entry, leaving the target date field manually editable.
If you use a standard calculation script under the 'Calculate' tab, the target field is forced into a read-only state because the form continuously recalculates it. By placing the calculation logic within the Validation event of the 'Order Date' or 'Article' field, the script is only triggered when the user modifies that specific field. This populates the dispatch date automatically while allowing the user to manually change it later if needed.
Open your PDF form in edit mode, right-click the 'Order Date' text field, and select 'Properties'.
Go to the 'Validate' tab within the properties window and select the option to run a custom validation script.
Enter the JavaScript code to verify both the Order Date and Article fields are filled out: var s1 = event.value; var s2 = this.getField('article1').valueAsString;
Add the logic to format the input date, add one day, and set the new dispatch date: if (s1!='' && s2!='') { var d1 = util.scand('dd/mm/yyyy', s1); d1.setDate(d1.getDate()+1); this.getField('dispatch1').value = util.printd('dd/mm/yyyy', d1); }
Click 'OK' to save the script, close the properties window, and test your form by entering an order date to see the dispatch date update dynamically.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editing environment that makes it simple to design interactive forms, insert text fields, and manage complex document properties for free.
- 1. Open the PDF Editor: Launch WPS Office and click on 'PDF Edit' to open your document.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to view all available interactive form controls.
- 3. Insert Text Fields: Click 'Text Field' and drag on the document area to create inputs for dates, names, or articles.
- 4. Edit Field Properties: Double-click any inserted field to rename it, adjust its appearance, or configure validation rules.

Frequently Asked Questions
Why can't I manually edit a field that has a calculation script applied?
When a field uses a calculation script under the 'Calculate' tab, the PDF viewer continuously evaluates the script and overwrites the field's value. This inherent behavior prevents any manual overrides. Moving the logic to a Validation or Keystroke event bypasses this restriction.
How does util.scand() work in PDF JavaScript?
The util.scand() method is a built-in Acrobat JavaScript function that parses a string into a JavaScript Date object based on a defined format string (like 'mm/dd/yyyy'). This allows developers to programmatically add days, compare dates, or reformat them.
What happens if the user enters the date in the wrong format?
If the entered date format does not match the exact format specified in the util.scand() function, the script will fail to parse the date and throw an error, preventing the dynamic calculation. To avoid this, set a strict format constraint in the field's 'Format' properties.




