PDF Editing Issues

Change a PDF Form Field Color for Multiple Values with JavaScript

Emma BrownEmma Brown Nov 19, 2026 868 views

Question details

Change a PDF form field's background color when its value matches any of several specified numbers.

Change a PDF Form Field Color for Multiple Values with JavaScript
Product
Adobe Acrobat PDF forms
Device & OS
not provided
Scenario
A user wants a PDF form field to turn red when the entered value is 9, 15, 21, 25, 27, or 29.
Observed behavior
The field should turn red for any matching value and return to a transparent background for other values.
Before you start

Open the PDF in a version of Adobe Acrobat that can edit form fields, and confirm that the target field is editable. Make sure JavaScript is enabled in Acrobat's Preferences under JavaScript.

Solution 1Recommended

Use a field validation script to check multiple values

Use a logical OR condition to turn the field red when any listed number is entered.

The logical OR operator (||) makes the condition true when any one comparison matches. Using && would require the field value to equal all the different numbers at once.

1
Open the form editor

In Adobe Acrobat, open the PDF and select Prepare a form from the tools menu.

2
Open field properties

Right-click the target field, select Properties, and open the Validate tab.

3
Add the script

Select Run custom validation script, click Edit, and enter: var v = Number(event.value); if (v == 9 || v == 15 || v == 21 || v == 25 || v == 27 || v == 29) { event.target.fillColor = color.red; } else { event.target.fillColor = color.transparent; }

4
Save and test

Close the script editor and field properties, save the PDF, and enter a listed number followed by a different number to check both colors.

Use a field validation script to check multiple values
Choose the nonmatching color: The else branch makes the field background transparent for nonmatching values. Replace color.transparent with another supported Acrobat color if you want a different default.
Free Microsoft Office alternative

Edit PDFs and Office documents with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and compatible Office files. It makes moving between document formats straightforward, but it is not a substitute for Acrobat's PDF form JavaScript editor.

Open and work with PDF documents in one office suiteCompatible with Microsoft Office formats including DOCX, XLSX, and PPTXUse a familiar interface for everyday document tasksMove between Office files and PDFs in a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Why should I use || instead of && in the condition?

Use || when any one value should trigger the color change. The && operator requires every comparison to be true, so one field value cannot simultaneously equal 9, 15, and the other listed numbers.

Where do I add the JavaScript in Acrobat?

Open Prepare a form, right-click the field, and select Properties. On the Validate tab, select Run custom validation script and click Edit.

How do I add another number to the condition?

In the field's custom validation script, add another OR comparison before the closing parenthesis, such as || v == 31.

Why doesn't the field change color when I test it?

Check that the script is attached to the intended field under Properties > Validate and that JavaScript is enabled in Acrobat's Preferences. Test the saved PDF in a viewer that supports Acrobat form JavaScript.