How to Validate Checkbox Values When Closing a PDF Using JavaScript
Question details
The user needs to validate the state of checkboxes and radio buttons automatically when a PDF document is closing, alerting the user if required fields are missing.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms where users must complete specific checkboxes before successfully closing the file.
- Observed behavior
- The user wants to display a warning alert if specific checkboxes or radio button combinations are left unselected upon document closure.
Ensure your PDF editor supports JavaScript execution for form fields and that you know the exact field names and export values of the checkboxes you intend to validate.
Use Acrobat JavaScript in the Document Will Close Event
Apply a custom JavaScript code snippet to the document's close action to evaluate field values logically and trigger a warning alert.
You can use JavaScript's logical operators to evaluate whether specific checkboxes are 'Off' or if a certain radio button choice has been selected. By binding this script to the 'Document Will Close' event, users will be prompted right before the file closes, ensuring they don't forget mandatory selections.
Open your PDF in an advanced PDF form-editing tool (such as Adobe Acrobat) and navigate to the Document JavaScript or Document Actions settings.
Locate the 'Document Will Close' event in the action list and click 'Edit' or 'Add' to insert a new JavaScript action.
Enter your validation script. For example: if (this.getField('CheckBox1').value === 'Off' || (this.getField('RadioButton').value === 'Choice1' && this.getField('CheckBox2').value === 'Off')) { app.alert('Not all checkboxes are selected!', 3, 0, 'Attention!'); }
Click 'OK' to save the script, then test the document by leaving a required checkbox blank and attempting to close the file to see if the alert triggers.

Create and Manage Interactive PDF Forms in WPS Office
WPS Office provides a comprehensive PDF editor that allows you to view, fill, and manage PDF forms seamlessly. With robust functionality and standard compliance, handling form data is efficient and straightforward.
- 1. Download and Install WPS Office: Download the free WPS Office suite from the official website and complete the installation process.
- 2. Open Your PDF Form: Launch WPS PDF and open your interactive PDF document that contains checkboxes and radio buttons.
- 3. Access Form Tools: Navigate to the 'Form' tab to interact with, fill out, or modify your PDF form fields with ease.
- 4. Save Your Document: After completing your form, save the document securely, maintaining full compatibility with all standard PDF readers.

Frequently Asked Questions
Why is my getField() JavaScript returning an error?
This usually happens if the field name is misspelled, capitalization is incorrect, or the field does not exist in the document. Open your form properties and verify the exact spelling of your checkbox and radio button names.
What is the export value of an unchecked checkbox in a PDF?
By default, in most standard PDF form specifications, the value of an unchecked checkbox is evaluated as the string 'Off'. Always ensure your script checks for this exact string.
Can I prevent the PDF from closing entirely using this script?
The 'Document Will Close' event allows you to execute scripts such as warning alerts to notify the user. However, standard security restrictions in most PDF viewers generally prevent scripts from entirely aborting or blocking the application's close action.




