Combine PDF Form Fields Without Extra Commas Using JavaScript
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.

- 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.
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.
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.
Open your PDF in form editing mode, right-click the target 'charges' field where you want the combined text to appear, and select 'Properties'.
In the Properties dialog box, click on the 'Calculate' tab, select the 'Custom calculation script' option, and click the 'Edit' button.
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(', ');
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.
Filter Out Empty Text using Regular Expressions
If fields appear blank but actually contain hidden spaces, checking the string length might fail. Regular expressions can accurately detect and ignore fields containing only whitespace.
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. Download and Install WPS Office: Get the latest version of WPS Office for free and launch the WPS PDF module.
- 2. Open Your PDF Form: Click on 'Open' to import your PDF document containing the form fields.
- 3. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all interactive form-filling tools.
- 4. Fill and Save: Easily type into text fields, check boxes, and export your completed PDF form flawlessly.

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.




