How to Change PDF Form Field Border Colors with JavaScript
Question details
Dynamically changing the border colors of text, drop-down, and date fields in a PDF form using JavaScript based on specific user inputs or field events.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Customizing interactive PDF forms to provide visual feedback to users by changing the form field's border color when specific data is entered or selected.
- Observed behavior
- Standard format scripts fail to update the stroke color on built-in Date fields, and drop-down fields require specific property adjustments to process typed values.
Ensure you have access to an advanced PDF editor that supports form creation and document-level JavaScript execution. Familiarize yourself with accessing the Properties dialog of PDF form fields.
Use Validation Events for Date Fields
Bypass limitations with built-in Date formatting by triggering the border color script through the Validation event.
A custom Format script is unavailable when a form field uses a built-in Date format. To successfully change the border color upon user entry, you must bind your script to a different event, such as the Validation event. This allows the strokeColor to update immediately after a valid date is entered.
Enter Form Editing mode in your PDF editor, right-click the target Date field, and select 'Properties' from the context menu.
Click on the 'Validate' tab located at the top of the Properties dialog box.
Select the option to 'Run custom validation script' and click 'Edit' to open the JavaScript editor.
Enter your code to update the border color (e.g., using event.target.strokeColor) based on the user's input, then click 'OK' to save.

Enable Custom Text Entry for Drop-down Fields
Adjust drop-down field properties to allow JavaScript to respond to custom typed values.
Centralize Code with Document-Level Scripts
Organize reusable functions for changing border colors by placing them in a document-level script.
Easily Create and Customize Interactive PDF Forms with WPS Office
WPS Office provides a robust PDF editor that simplifies the process of creating, editing, and managing interactive form fields. With an intuitive interface, you can effortlessly adjust field properties, appearances, and formatting without needing complex scripting for basic customization.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document. Switch to the 'PDF Edit' workspace.
- 2. Access Form Tools: Click on the 'Form' tab located on the top ribbon to view all available interactive form controls.
- 3. Insert Form Fields: Select the desired field type, such as Text Field or Drop-down Menu, and click on the document to place it.
- 4. Customize Appearance: Double-click the inserted field to open its Properties dialog, where you can directly modify border colors and appearance settings.
- 5. Save the Document: Once your fields are configured, save your PDF to preserve all interactive elements and formatting.

Frequently Asked Questions
Why doesn't my JavaScript change the border color of a formatted Date field?
When a field uses a built-in Date format, a custom Format script cannot be executed simultaneously. To resolve this, attach your JavaScript to another event, such as the Validation event, to change the field's strokeColor after a value is entered.
How do I ensure my script works when a user types a custom value into a drop-down field?
For drop-down fields, you must enable the option that permits custom text entry. Right-click the drop-down field, open Properties, navigate to the Options tab, and check 'Allow user to enter custom text'.
What JavaScript property controls the border color of a PDF form field?
In Acrobat JavaScript, the border color of a field is controlled by the 'strokeColor' property. You can change it by assigning a color array to this property, such as event.target.strokeColor = color.red;.




