How to Prevent PDF JavaScript from Concatenating Blank Form Values
Question details
The user needs to stop Adobe Acrobat JavaScript from joining (concatenating) PDF form values into a long text string when performing calculations with blank fields.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Creating a custom JavaScript calculation in a fillable PDF form where some referenced fields might be left empty by the user.
- Observed behavior
- When adding form fields using a JavaScript custom calculation, blank fields cause the script to treat all values as strings, resulting in concatenated text (e.g., 17,000500500500) instead of a mathematically correct numeric total.
Ensure your PDF editor supports interactive form field properties and custom JavaScript execution, and verify the exact names of the form fields you wish to calculate.
Explicitly Convert Field Values Using the Number() Function
Wrap each form field variable inside the JavaScript Number() function to force the program to treat blank strings as zero and correctly add them numerically.
By default, PDF JavaScript extracts form field values as text strings. If you try to add these values together using the plus (+) operator, JavaScript concatenates the text if any field is empty or blank. Using the Number() function explicitly casts the extracted string into a numeric value, treating empty fields as 0 and ensuring mathematical addition instead of string concatenation.
Open your PDF in form editing mode, right-click the field where you want the total to appear, and select 'Properties'.
In the Properties dialog box, navigate to the 'Calculate' tab and ensure 'Custom calculation script' is selected.
Click 'Edit' to open the JavaScript editor. Wrap every variable representing a form field in the Number() function. For example, change 'Total + Down + Fee' to 'Number(Total) + Number(Down) + Number(Fee)'.
To keep the total field blank when no values are entered, structure your script with an if/else statement: if (Number(Total) > 0) { event.value = Number(Total) + Number(Down) + Number(Fee); } else { event.value = ""; }

Assign Default Numeric Values to Empty Fields
Set the default value of all calculated form fields to zero to prevent the script from encountering empty strings altogether.
Edit and Calculate Fillable PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to effortlessly design interactive forms, configure complex JavaScript calculations, and process data accurately without unexpected string concatenation.
- 1. Open Your PDF in WPS Office: Download and launch WPS Office, then open your interactive PDF document containing the form fields.
- 2. Navigate to Form Tools: Switch to the 'Form' tab on the top ribbon to access the interactive field creation and editing features.
- 3. Access Field Properties: Right-click the text field designated for your total sum, select 'Properties', and navigate to the 'Calculate' tab.
- 4. Input Your Script: Select 'Custom calculation script', click 'Edit', and input your JavaScript formula using the Number() function to ensure accurate mathematics.
- 5. Save and Test: Click 'OK', exit the form editing mode, and test your document by leaving some fields blank to confirm the calculation works.

Frequently Asked Questions
Why does JavaScript add my PDF form fields as a long text string instead of a number?
JavaScript uses dynamic typing. Because PDF form inputs are extracted as text by default, the plus (+) operator serves a dual purpose: addition for numbers and concatenation for strings. When it encounters a blank string, it assumes you want to join the text together rather than perform math.
Are there other ways to convert strings to numbers besides the Number() function?
Yes, multiplying the field value by 1 (e.g., Field1 * 1) forces an implicit conversion to a numeric value. However, wrapping the variable in the explicit Number() function is widely considered safer and more readable for complex PDF scripts.
Does the Number() function work accurately with decimal and currency values?
Yes, the Number() function in JavaScript successfully processes both whole integers and floating-point numbers (decimals). It is highly suitable for financial totals and currency fields within PDF forms.




