Fix JavaScript Conditional Logic for PDF Form Fields
Question details
The user needs to correct a custom calculation script for a PDF form to dynamically combine two renter names when both are filled, or output a default label when the second field is empty.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Writing a custom JavaScript calculation script in a PDF form to concatenate field values based on specific conditional logic.
- Observed behavior
- The original script fails to execute properly due to syntax errors such as incorrect comparisons, missing braces, a stray semicolon after the if condition, and a failure to assign the result to the event value.
Ensure you have a PDF editor that supports JavaScript custom calculation scripts and that the specific text fields you are trying to reference have already been created with the exact matching names.
Fix the Custom Calculation Script Syntax
Use the correct JavaScript syntax to check the field values as strings, apply the conditional logic, and explicitly assign the final output to the event.value property.
JavaScript in PDF forms requires precise syntax. A common mistake is placing a semicolon directly after an 'if' statement condition, which prematurely terminates the logic before the code block executes. Additionally, PDF form calculation scripts must explicitly assign the desired output to 'event.value' for the text to appear in the field.
Use the 'this.getField("FieldName").valueAsString' method to fetch the input values as text strings. For example, create variables for the fields by writing: 'var R = this.getField("Renter").valueAsString;' and 'var R2 = this.getField("Renter2").valueAsString;'.
Write an 'if' statement to check if the second renter field is not empty using strict inequality: 'if (R2 !== "") { ... }'. Ensure there is no semicolon immediately after the closing parenthesis of the 'if' condition.
Inside the 'if' block, assign the combined names to the event output: 'event.value = R + " and " + R2;'. In the 'else' block, assign the fallback default text: 'event.value = "Lessee";'.
Open your PDF form editor, right-click the target field, select 'Properties', navigate to the 'Calculate' tab, choose 'Custom calculation script', and paste your corrected JavaScript.
Easily Create and Manage Interactive PDF Forms in WPS Office
WPS Office provides robust PDF editing capabilities, allowing you to create interactive forms, configure text fields, and manage document properties with ease. It's the perfect tool for standardizing your PDF workflows.
- 1. Open your PDF in WPS Office: Launch WPS Office and open the PDF form document you want to edit or customize.
- 2. Access the Form tab: Navigate to the 'Form' tab on the top ribbon to view all interactive form controls, such as text fields, checkboxes, and buttons.
- 3. Edit field properties: Double-click on any text field to open its properties panel, where you can modify the field name, appearance, and calculation settings.

Frequently Asked Questions
Why isn't my PDF JavaScript calculation updating the field?
Ensure that your script assigns the final result to 'event.value'. If you omit this assignment, the calculation will process in the background, but the field itself will remain blank. Also, check the field calculation order in your PDF editor if multiple fields depend on each other.
What does valueAsString do in a PDF script?
The 'valueAsString' property specifically retrieves the content of a PDF form field as a text string. This prevents unexpected JavaScript type coercion errors, especially when concatenating text or checking if a field is completely empty.
How do I debug JavaScript errors in a PDF form?
Review your code for common syntax errors, such as missing curly braces '{}' or stray semicolons ';' immediately following an 'if' condition. Most advanced PDF editors also include a JavaScript console where you can test code snippets and view runtime error logs.
Can I use this same logic for date or number calculations?
This specific conditional logic is intended for combining text strings. If you need to perform mathematical calculations or format dates, you should parse the field values as 'Number' or 'Date' objects instead of using 'valueAsString'.




