How to Distinguish Empty and Invalid Text Fields in PDF Using Acrobat JavaScript
Question details
The user needs to use JavaScript in a PDF form to differentiate between a text field that has been left blank and one that contains an incorrect value.

- Product
- PDF / Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Validating user input within interactive PDF forms.
- Observed behavior
- The goal is to accurately identify when a field is filled but incorrect, without triggering the same error condition for a completely empty field.
Ensure you have access to the PDF form editing tools and basic knowledge of JavaScript syntax to apply custom validation scripts.
Use Compound Logical Operators for Field Validation
Apply a conditional statement that checks both if the field is not equal to the expected answer and not equal to an empty string.
To properly validate the field, you cannot simply check if the value is different from the correct answer, because an empty string also meets that criteria. By combining two conditions using the logical AND (&&) operator, you ensure the script only targets cells that contain text but do not match the expected answer.
Open your PDF form in editing mode, right-click the target text field, and select 'Properties'.
Go to the 'Calculate' or 'Validate' tab, depending on when you want the validation to occur, and choose 'Run custom validation script'.
Click 'Edit' and enter your script using the format: `if (event.value !== "Answer1" && event.value !== "") { // Error handling code here }`.
Click 'OK' to close the JavaScript window, close the properties dialog, and save your PDF document to test the form.

Create and Edit PDF Forms with WPS Office
WPS Office provides comprehensive PDF editing tools, allowing you to easily create interactive forms, add text fields, and manage document properties without needing expensive software.
- 1. Install WPS Office: Download and install WPS Office, then open your existing PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the interactive form field tools.
- 3. Insert Text Fields: Click on 'Text Field' and drag to draw a new input area on your document, then double-click to configure its properties.
- 4. Save the Interactive Form: Once your fields are set up, click 'Save' or 'Export to PDF' to distribute your interactive form.

Frequently Asked Questions
Why does checking for an incorrect answer also flag empty fields?
If you only check `if (fieldValue !== 'CorrectAnswer')`, an empty field ('') also evaluates to true because a blank space is not equal to 'CorrectAnswer'. You must explicitly exclude empty strings to avoid triggering the incorrect answer logic on a blank field.
What is the difference between `!=` and `!==` in PDF JavaScript?
`!=` checks for inequality with type conversion, while `!==` (strict inequality) checks that both the value and the type are not identical. Using strict inequality is generally safer and more precise for text input validations.
Can I apply this validation logic to multiple fields at once?
Yes, you can iterate through multiple fields using a loop and apply the same `&&` logic, or write a document-level JavaScript function that accepts the field name and expected answer as parameters for reuse across multiple fields.




