PDF Editing Issues

Combine PDF Form Fields Without Extra Commas Using JavaScript

John WilsonJohn Wilson Nov 5, 2026 870 views

Question details

The user needs to combine multiple PDF form fields (crg1 through crg6) into a single 'charges' field separated by commas, ensuring that blank fields do not generate extra or trailing commas.

Combine PDF Form Fields Without Extra Commas Using JavaScript
Product
Adobe Acrobat / PDF Forms
Device & OS
not provided
Scenario
Setting up a custom calculation script in a PDF form to merge text from several input fields into one master field formatted as a comma-separated list.
Observed behavior
Standard string concatenation adds commas for every field, resulting in poorly formatted text with consecutive or trailing commas (e.g., 'item1, item2, , , ') when some fields are left blank.
Before you start

Ensure your PDF editor supports advanced form field editing and JavaScript calculations. Create a backup copy of your PDF form before modifying the calculation scripts to prevent accidental data loss.

Solution 1Recommended

Use JavaScript Array Push and Join Methods

Using an array to collect non-empty values and then joining them with a comma ensures that separators are only placed between valid text strings.

Standard string concatenation using the '+' operator applies separators blindly, leading to extra commas if the referenced field is empty. Instead, we can use a loop to check each field's value.

If a field contains text, it is pushed into a temporary array. Finally, the JavaScript '.join()' method combines all array elements into a single string, automatically placing commas only between the elements.

1
Open Field Properties

Open your PDF in form editing mode, right-click the target 'charges' field where you want the combined text to appear, and select 'Properties'.

2
Navigate to the Calculate Tab

In the Properties dialog box, click on the 'Calculate' tab, select the 'Custom calculation script' option, and click the 'Edit' button.

3
Input the JavaScript Code

Enter the following script: var values = []; for (var i = 1; i < 7; i++) { var field = this.getField('crg' + i); if (field && field.value.toString().length > 0) values.push(field.value.toString()); } event.target.value = values.join(', ');

4
Save and Test

Click 'OK' to save the script and close the properties window. Test the form by filling out only some of the 'crg' fields to verify that no extra commas appear.

Dynamic Formatting: The .join(', ') method is extremely versatile. You can easily change the separator by modifying the characters inside the quotation marks, such as using '\n' for new lines.
Advanced PDF Form Management

Edit and Manage PDF Forms with WPS Office

While complex JavaScript calculations are specific to advanced scripting setups, WPS Office provides an incredibly user-friendly PDF editing module. You can effortlessly view, fill, organize, and manage PDF forms without needing to write code.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office for free and launch the WPS PDF module.
  2. 2. Open Your PDF Form: Click on 'Open' to import your PDF document containing the form fields.
  3. 3. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all interactive form-filling tools.
  4. 4. Fill and Save: Easily type into text fields, check boxes, and export your completed PDF form flawlessly.
Seamlessly fill and manage interactive PDF forms.Fully compatible with standard PDF formats created in Adobe Acrobat and Microsoft Office.Lightweight, fast, and completely free to download.Comprehensive PDF editing tools including text modification, highlighting, and page organization.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get trailing commas when combining fields in Acrobat?

Trailing commas happen when you use standard string concatenation (e.g., field1 + ',' + field2) and one of the fields is empty. The calculation script adds the comma regardless of whether the field actually contains data.

How do I access the custom calculation script in a PDF form?

Enter the form editing mode of your PDF software, right-click the specific text field where you want the result to appear, select 'Properties', navigate to the 'Calculate' tab, and choose 'Custom calculation script'.

What is the advantage of using the .join() method in JavaScript?

The .join() method takes all valid elements in an array and seamlessly stitches them together using the separator you define. It intelligently skips adding separators before the first element or after the last element, fully eliminating the problem of trailing commas.