Change an Acrobat Form Field Color When Three Values Match
Question details
Set an Acrobat form field's background color according to whether three calculated fields contain the same numeric value.

- 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.
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.
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.
In Acrobat, open the PDF, select All tools > Prepare a form, and double-click the Totals field.
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.
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;
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.

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.

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.




