Combine PDF Form Field Values Without Extra Separators
Question details
The user needs to merge multiple PDF form fields into a single string using JavaScript, ensuring no trailing or duplicate separators appear if a specific field is left empty.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating a composite text field (such as a full address) that automatically updates based on user input from other individual text fields.
- Observed behavior
- Direct concatenation of fields results in unwanted commas and spaces when optional fields (like City or State) are left blank by the user.
Ensure you have access to a PDF editor that supports document-level JavaScript and form field editing, and carefully verify the exact spelling of your target field names.
Use JavaScript Arrays to Combine Non-Empty Values
Collect non-empty field values into an array and use the JavaScript join method to cleanly separate them, avoiding dangling commas.
When calculating field values in a PDF using JavaScript, passing each individual field name to getField allows you to verify its content before concatenation.
By pushing only the non-empty valueAsString properties to an array, the join method will automatically insert separators exclusively between the values that actually exist.
Open your PDF form editor, right-click the target combined text field, and select 'Properties'.
Navigate to the Calculate tab, select the 'Custom calculation script' option, and click 'Edit'.
Create an array containing the exact names of the fields you want to combine. For example: var fields = ["Case City", "Case State", "Case Zip Code"];
Create an empty array for the values. Loop through the fields array using 'this.getField(fields[i])' to access each specific element.
Check if 'valueAsString' is not empty before pushing it to your values array. Finally, set 'event.value = values.join(", ");' to display the formatted text.
Manage and Fill PDF Forms with WPS Office
WPS Office provides a highly capable PDF toolset that allows you to easily view, fill, and manage interactive PDF forms without needing heavy or expensive software.
- 1. Download WPS Office: Download and install the latest version of WPS Office from the official website.
- 2. Open your PDF file: Launch WPS PDF and open the document containing the form fields you need to manage.
- 3. Interact with forms: Navigate to the Form tab to input data, modify standard field layouts, or safely view dynamically calculated values.
- 4. Save and export: Save your filled PDF document locally or export it to other supported Microsoft Office formats if further editing is required.

Frequently Asked Questions
Why do I get an error saying the field does not exist in the JavaScript console?
This usually happens if there is a typo in the field name within your JavaScript array. Double-check the exact spelling, capitalization, and spacing of the field names in your PDF's form properties.
Can I use different separators instead of a comma and a space?
Yes. By changing the argument in the values.join() method, you can use any separator you prefer. For example, values.join(" - ") will separate your text fields with a hyphen.
Why are my empty form fields leaving trailing commas in the final text?
Trailing commas appear when you concatenate string fields directly (e.g., field1 + ", " + field2) without checking if they contain data first. Using an array and the join method ensures separators are only added between actual non-empty values.




