Compare PDF Form Field Values and Highlight Duplicates in Adobe Acrobat
Question details
Create Acrobat JavaScript that compares PDF form field values and visually identifies duplicate entries.

- Product
- Adobe Acrobat PDF forms
- Device & OS
- not provided
- Scenario
- A calendar or billing form contains multiple fields where users enter district numbers.
- Observed behavior
- Repeated nonempty values should appear in red, while unique values should return to the normal text color.
Open the PDF in Adobe Acrobat and check the exact names of the text fields in Prepare a Form. Make sure Acrobat JavaScript is enabled and that you can edit the form and its scripts.
Use Acrobat JavaScript to Highlight Duplicate Field Values
Use a document-level function and field validation scripts to check entries whenever a listed field is edited.
The example compares values as text after removing leading and trailing spaces. It ignores empty fields, marks every field containing a duplicate in red, and sets all other listed fields to black.
In Acrobat, open All tools > Use JavaScript > Document JavaScripts. Create a script and paste the code below, replacing the three example names with your actual field names: function checkDuplicates(changedName, pendingValue) { var names = ["District1", "District2", "District3"]; var fields = []; var values = []; var i, j; for (i = 0; i < names.length; i++) { fields[i] = this.getField(names[i]); if (!fields[i]) continue; var value = names[i] === changedName ? pendingValue : fields[i].valueAsString; values[i] = String(value == null ? "" : value).replace(/^\s+|\s+$/g, ""); } for (i = 0; i < names.length; i++) { if (!fields[i]) continue; var duplicate = false; if (values[i] !== "") { for (j = 0; j < names.length; j++) { if (i !== j && fields[j] && values[i] === values[j]) { duplicate = true; break; } } } fields[i].textColor = duplicate ? color.red : color.black; } }
Open Prepare a Form, right-click each listed field, and choose Properties > Validate. Select Run custom validation script, click Edit, and enter: checkDuplicates(event.target.name, event.value);
Save the PDF, close and reopen it in Acrobat, then enter the same value in two listed fields and leave the second field. Both entries should turn red. Change or clear one value and leave the field to confirm the colors reset.

Work With PDF and Office Files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and compatibility with common Microsoft Office formats, making it easy to move between documents. Use Adobe Acrobat for the JavaScript-based duplicate check described above; WPS Office does not fix Acrobat script behavior.

Frequently Asked Questions
Why does the script ignore blank fields?
It removes leading and trailing spaces and compares only nonempty values, so blank fields are not treated as duplicates.
Where do I find the field names to use in the script?
Open Prepare a Form in Acrobat and inspect the field list or open a field's Properties. Replace the example names in the names array with the exact names from your form.
Will the highlight disappear when I correct a duplicate?
Yes. When a listed field passes validation, the script checks all listed fields again and changes unique or empty entries to black.
Why does the duplicate check not work in my PDF viewer?
The viewer may not support Acrobat JavaScript, or JavaScript may be disabled. Test the saved form in Acrobat with JavaScript enabled and verify that each listed field has the validation script.




