PDF Editing Issues

How to Change PDF Form Field Border Color with JavaScript

Muhammad TalhaMuhammad Talha Nov 24, 2026 868 views

Question details

The user needs to change the border color of a fillable PDF form field dynamically based on whether it is empty or contains text.

How to Change PDF Form Field Border Color with JavaScript
Product
Adobe Acrobat / PDF
Device & OS
not provided
Scenario
Setting up visual validation in an interactive PDF form so users can easily see which fields are filled and which are missing data.
Observed behavior
The goal is to automatically turn the form field border blue when text is entered, and red when the field is left empty.
Before you start

Ensure you have Adobe Acrobat Pro or an equivalent advanced PDF form editor that supports custom JavaScript for field validation.

Solution 1Recommended

Use a Custom Validation or Format Script

Add a custom JavaScript snippet to the Calculate or Format tab in the field properties to change the border color dynamically.

This method uses an if/else statement to check if the field contains any text. If it does, the border turns blue; if it doesn't, it turns red. This provides immediate visual feedback to the user.

1
Open Field Properties

Open your PDF form in edit mode, right-click the target form field, and select 'Properties'.

2
Navigate to the Script Tab

Go to the 'Calculate' or 'Format' tab within the Properties window.

3
Select Custom Script

Choose the option to enter a 'Custom Script'.

4
Enter the JavaScript Code

Type or paste the following code: `if (event.value && event.value.toString().length > 0) { event.target.strokeColor = color.blue; } else { event.target.strokeColor = color.red; }` and save your changes.

Use a Custom Validation or Format Script
Test Your Form: Switch back to Preview or regular viewing mode to type in the field and verify the border color changes appropriately.
Easily Manage PDF Forms

Create and Fill PDF Forms with WPS PDF

WPS Office offers a robust PDF editor that allows you to easily view, fill, and manage interactive PDF forms without the complexity of heavy software. It provides a seamless reading and editing experience.

  1. 1. Download WPS Office: Install WPS Office for free from the official website to access the built-in PDF tools.
  2. 2. Open Your PDF: Launch WPS PDF and open your interactive PDF form document.
  3. 3. Fill the Form Fields: Click directly into the fillable fields to input your data. WPS PDF automatically recognizes standard form fields.
  4. 4. Save or Export: Once filled, click 'Save' or export your document to securely store your entered information.
Fully compatible with standard interactive PDF form fieldsFree, lightweight, and incredibly easy to useSeamless format compatibility with Microsoft Word, Excel, PowerPoint, and standard PDFsMulti-platform support for Windows, Mac, and mobile devices
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my JavaScript working in the PDF form?

Ensure that JavaScript is enabled in your PDF viewer's preferences. Additionally, double-check your code for typos and verify that you placed the script in the correct tab (Validation, Format, or Calculate) within the field properties.

Can I use colors other than red and blue for PDF form borders?

Yes. Acrobat JavaScript supports various predefined colors like color.green, color.black, or color.yellow. You can also define custom colors using an array like ["RGB", 1, 0.5, 0].

Does WPS PDF support filling out interactive forms?

Yes, WPS PDF allows users to open, view, and fill out standard interactive PDF forms quickly and easily.

How do I remove the border from a PDF form field completely?

You can set the border color to transparent by setting `event.target.strokeColor = color.transparent;` in your JavaScript, or by selecting 'No Color' in the Appearance tab of the field properties.