PDF Editing Issues

How to Prevent PDF JavaScript from Concatenating Blank Form Values

Rana GarciaRana Garcia Nov 10, 2026 869 views

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.

How to Prevent Acrobat JavaScript from Concatenating Blank Form Values
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Field Properties

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

2
Access the Calculation Tab

In the Properties dialog box, navigate to the 'Calculate' tab and ensure 'Custom calculation script' is selected.

3
Modify the JavaScript

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)'.

4
Implement Conditional Logic

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 = ""; }

Explicitly Convert Field Values Using the Number() Function
Calculation Successful: The PDF will now properly evaluate empty fields as mathematical zeros, outputting the correct sum.
Efficient PDF Form Editing

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. 1. Open Your PDF in WPS Office: Download and launch WPS Office, then open your interactive PDF document containing the form fields.
  2. 2. Navigate to Form Tools: Switch to the 'Form' tab on the top ribbon to access the interactive field creation and editing features.
  3. 3. Access Field Properties: Right-click the text field designated for your total sum, select 'Properties', and navigate to the 'Calculate' tab.
  4. 4. Input Your Script: Select 'Custom calculation script', click 'Edit', and input your JavaScript formula using the Number() function to ensure accurate mathematics.
  5. 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.
Easily create, edit, and fill interactive PDF forms with advanced custom calculation scriptsFull compatibility with standard PDF JavaScript APIs and Adobe Acrobat form formatsLightweight, fast-loading, and incredibly user-friendly interfaceCost-effective Microsoft Office and Adobe Acrobat alternative
microsoft office alternative - wps office

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.