PDF Editing Issues

Compare a Field Value with an Array in an Acrobat PDF Form

Huma Ashraf ChHuma Ashraf Ch Nov 19, 2026 868 views

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.

Compare a Field Value with an Array in an Acrobat PDF Form
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the form editor

Open the PDF in Acrobat, select Prepare a form, and open the properties of the field you want to check.

2
Add a validation script

On the Validate tab, select Run custom validation script and click Edit.

3
Enter the array check

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;

4
Save and test

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.

Use indexOf in an Acrobat field validation script
Match values exactly: indexOf uses strict equality. Text comparisons are case-sensitive, and a text value such as "1" does not match the number 1.
Free Microsoft Office alternative

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.

Open and review PDF documents alongside your Office filesWork with Microsoft Word, Excel, and PowerPoint formats, including DOCX, XLSX, and PPTXUse a familiar interface to move between documents, spreadsheets, and presentationsGet started with a free, lightweight office suite
microsoft office alternative - wps office

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.