PDF Editing Issues

Change an Acrobat Form Field Color When Three Values Match

Aamir Naveed AkramAamir Naveed Akram Nov 24, 2026 869 views

Question details

Set an Acrobat form field's background color according to whether three calculated fields contain the same numeric value.

Change an Acrobat Form Field Color When Three Values Match
Product
Adobe Acrobat
Device & OS
not provided
Scenario
A PDF form has a Totals field that should show green when three calculated values match and red otherwise.
Observed behavior
The existing JavaScript uses chained assignment and comparison incorrectly, so it does not reliably compare all three values.
Before you start

Open the form in Adobe Acrobat and check the exact names of the three fields in Prepare a Form. Confirm that their values are plain numbers and whether zero is a valid value.

Solution 1Recommended

Compare the Three Values in an Acrobat Calculation Script

Use separate numeric comparisons to update the Totals field color whenever the form recalculates.

The example treats blank or nonnumeric fields as a mismatch, but accepts zero as a valid number. Replace FieldA, FieldB, and FieldC with the exact names of your three calculated fields.

1
Open the field properties

In Acrobat, open the PDF, select All tools > Prepare a form, and double-click the Totals field.

2
Edit the calculation script

On the Calculate tab, select Custom calculation script and click Edit. If Totals already has a calculation, preserve its calculation logic in the custom script rather than replacing it without restoring that logic.

3
Add the comparison

Add this code after any existing calculation logic: var s1 = this.getField("FieldA").valueAsString; var s2 = this.getField("FieldB").valueAsString; var s3 = this.getField("FieldC").valueAsString; var blank = /^\s*$/; var n1 = Number(s1); var n2 = Number(s2); var n3 = Number(s3); var matches = !blank.test(s1) && !blank.test(s2) && !blank.test(s3) && isFinite(n1) && isFinite(n2) && isFinite(n3) && n1 === n2 && n2 === n3; event.target.fillColor = matches ? color.green : color.red;

4
Check the calculation order

In Prepare a form, open More > Set Field Calculation Order and place Totals after the three fields it reads. Save the PDF, then test matching, differing, blank, and zero values.

Compare the Three Values in an Acrobat Calculation Script
Check number formatting: The comparison expects plain numeric values. Formatted strings such as currency symbols or comma-separated numbers may need to be normalized before conversion.
Free Microsoft Office alternative

Work With PDFs and Office Files in WPS Office

WPS Office is a free, lightweight, easy-to-use alternative for everyday document and PDF work. Its familiar interface and Office-format compatibility make it easy to move existing files into your workflow. It does not fix Acrobat's form JavaScript.

Open and review PDF documents alongside your other filesWork with Microsoft Word, Excel, and PowerPoint formats, including DOCX, XLSX, and PPTXUse a familiar interface for a smoother transition from Microsoft OfficeKeep common document tools together in a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why shouldn't I use a truthiness check for these values?

A truthiness check treats the number 0 as false. Test the field strings for blanks separately, then convert them to numbers so three zeros can count as a match.

Why does the Totals color update before the other fields?

In Prepare a form, open More > Set Field Calculation Order and move Totals below the three calculated fields it reads.

What happens if one of the fields is blank?

In the example script, a blank field makes the comparison fail and sets the Totals background to red.

Can I use this script if Totals already has a calculation?

Yes, but preserve the existing calculation logic when adding the color comparison. If Totals uses a built-in calculation option, incorporate its equivalent calculation into the custom script before switching options.