How to Fix Acrobat JavaScript If-Else Conditions for Text Fields
Question details
The user needs to fix a PDF form script where if-else conditions comparing text fields ('Thin' or 'Thick') and month fields produce undefined or incorrect results.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Calculating a numerical value in a PDF form using custom JavaScript based on the inputs from a months field and a thickness text field.
- Observed behavior
- The JavaScript comparisons produce undefined or incorrect results due to improper string comparison, data type mismatches, or missing boundary checks in the if-else logic.
Before modifying your JavaScript form code, ensure you save a backup copy of your PDF file to prevent accidental loss of complex logic or formatting.
Normalize String Cases and Explicitly Convert Numeric Fields
Fix logic errors by forcing numeric fields into Number types and normalizing text fields to upper or lower case before checking conditions.
In JavaScript, comparing a number stored as a string against an actual integer can yield unexpected results. Similarly, text fields are case-sensitive. Standardizing the formatting immediately upon retrieval ensures your if-else conditions evaluate accurately.
Wrap your numeric field extraction in the Number() function to ensure it evaluates as an integer. For example: var n = Number(this.getField('Months').valueAsString);
Use valueAsString to read the text field and append .toUpperCase() to standardize the string case. For example: var c = this.getField('Thickness').valueAsString.toUpperCase();
Compare the normalized text with exact quoted uppercase strings like 'THIN' or 'THICK'. Ensure you use correct mathematical operators for numeric ranges without omitting numbers like 11 or 17.
End your if-else block with a final else condition, such as else { event.value = 'ERROR'; }, to easily identify when an input does not meet any of your specified conditions.

Easily Fill and Manage PDF Forms with WPS Office
If you are looking for a reliable, fast, and user-friendly tool to handle your PDF forms, WPS PDF provides a comprehensive set of features for filling, editing, and managing your documents without complex scripting.
- 1. Open your PDF: Launch WPS Office and open your interactive PDF form.
- 2. Interact with the form: Click directly on the highlighted text fields, checkboxes, or dropdowns to input your data.
- 3. Save your filled document: Once completed, click 'Menu' and select 'Save' or 'Save As' to preserve your inputs securely.

Frequently Asked Questions
Why is my Acrobat JavaScript returning 'undefined'?
This commonly happens when a script attempts to read a field that is empty, or references a field name incorrectly. Using .valueAsString ensures you retrieve an empty string instead of an undefined object if the field is left blank.
How do I make text comparison case-insensitive in PDF JavaScript?
You can temporarily convert the field value to all uppercase or all lowercase using .toUpperCase() or .toLowerCase() before comparing it with a capitalized or lowercase string in your if-else statement.
Why are my greater-than and less-than ranges failing in Acrobat forms?
Numeric ranges often fail if the field value is read as a string. Wrapping your field extraction in Number() explicitly casts it as an integer or float, allowing mathematical operators like < and > to function correctly.




