Change a PDF Form Field Color for Multiple Values with JavaScript
Question details
Change a PDF form field's background color when its value matches any of several specified numbers.

- 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.
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.
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.
In Adobe Acrobat, open the PDF and select Prepare a form from the tools menu.
Right-click the target field, select Properties, and open the Validate tab.
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; }
Close the script editor and field properties, save the PDF, and enter a listed number followed by a different number to check both colors.

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.

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.




