Compare a Field Value with an Array in an Acrobat PDF Form
Question details
Check whether a PDF form field value is included in a JavaScript array and set the field border color based on the result.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A form author wants to validate a field value against a list of allowed values.
- Observed behavior
- The field border should turn black when the value is in the array and red when it is not.
Open the PDF in an Acrobat edition that can edit form fields, and identify the field and values you want to check. Make sure JavaScript is enabled in Acrobat's JavaScript preferences.
Use indexOf in an Acrobat field validation script
Check the field value against an array whenever Acrobat validates the field, then set its border color.
Open the PDF in Acrobat, select Prepare a form, and open the properties of the field you want to check.
On the Validate tab, select Run custom validation script and click Edit.
In the JavaScript editor, enter: var array1 = ["A", "B", "C"]; var X = event.value; var isIncluded = array1.indexOf(X) !== -1; event.target.strokeColor = isIncluded ? color.black : color.red;
Replace the example array values with your own, click OK to save the script, and close the field properties. Exit form editing and enter values that are both in and outside the array to test the border color.

Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office documents. It supports common Microsoft Office formats for easier migration, but it does not replace Acrobat's form JavaScript editor.

Frequently Asked Questions
What does indexOf(X) return when the value is not in the array?
It returns -1. Use array1.indexOf(X) !== -1 to produce true when the value is found and false when it is not.
Where do I add the JavaScript in Acrobat?
Open Prepare a form, open the field's properties, and select Validate > Run custom validation script > Edit.
Why does a value in my array still produce a red border?
Check capitalization and value types. For example, "A" does not match "a", and the text "1" does not match the number 1.
Will the border color change in every PDF viewer?
Not necessarily. The viewer must support and allow the relevant Acrobat JavaScript. Test the saved form in the viewer your recipients will use.




