PDF Editing Issues

How to Validate Checkbox Values When Closing a PDF Using JavaScript

Elise WilliamsElise Williams Nov 5, 2026 869 views

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.

How to Validate Checkbox Values When a PDF Closes
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Document Actions

Open your PDF in an advanced PDF form-editing tool (such as Adobe Acrobat) and navigate to the Document JavaScript or Document Actions settings.

2
Edit the Document Will Close Event

Locate the 'Document Will Close' event in the action list and click 'Edit' or 'Add' to insert a new JavaScript action.

3
Insert Validation Script

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!'); }

4
Save and Test

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.

Use Acrobat JavaScript in the Document Will Close Event
Verify Field Names: If the script fails to run, double-check that your field names exactly match the strings in your getField() function. Keep in mind that the default export value for an unchecked box is the string 'Off'.
Edit PDF Forms Easily

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. 1. Download and Install WPS Office: Download the free WPS Office suite from the official website and complete the installation process.
  2. 2. Open Your PDF Form: Launch WPS PDF and open your interactive PDF document that contains checkboxes and radio buttons.
  3. 3. Access Form Tools: Navigate to the 'Form' tab to interact with, fill out, or modify your PDF form fields with ease.
  4. 4. Save Your Document: After completing your form, save the document securely, maintaining full compatibility with all standard PDF readers.
Fully compatible with standard Adobe PDF forms and Microsoft Office file formatsIntuitive form filling and editing interfaceLightweight, fast, and free to downloadCross-platform support for Windows, Mac, and mobile devices
microsoft office alternative - wps office

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.