How to Use JavaScript to Auto-Select PDF Checkboxes Based on Text Input
Question details
The user wants to automate a PDF form so that entering a value starting with 1, 2, 3, or 4 in a text field automatically selects a specific corresponding checkbox.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form to speed up data entry and ensure corresponding options are checked based on user input.
- Observed behavior
- The user needs to write and apply a JavaScript validation script that extracts the first character of the text field and triggers the correct checkbox state.
Make a note of the exact field names for your source text field and the four target checkboxes. Ensure your PDF editing software supports form field JavaScript validation before proceeding.
Add a Custom Validation Script to the Text Field
Use a custom JavaScript validation script triggered by the text field's input to automatically evaluate the first character and check the correct box.
By applying a custom script to the 'Validate' trigger of a text field, the PDF evaluates the input immediately after the user finishes typing. The script should first clear all target checkboxes to prevent multiple selections, then read the first digit, and finally apply a switch statement to check the matching box.
Open your interactive PDF in your PDF editor. Navigate to the 'Form' or 'Prepare Form' tab to access the form field layout and ensure your text field and checkboxes already exist.
Right-click on the source text field where the user will type the number, and select 'Properties' from the context menu.
Navigate to the 'Validate' tab within the Properties dialog. Check the option for 'Run custom validation script' and click the 'Edit' button. Enter a script that first resets the checkboxes, checks if the field is blank, and then uses 'event.value.slice(0, 1)' to read the first character. Use a switch statement to check the corresponding box using 'this.getField("YourCheckboxName").value = "Yes";'.
Click 'OK' to save the script, then close the Properties dialog. Exit form editing mode and type a number starting with 1, 2, 3, or 4 into the text field to verify that the correct checkbox is automatically selected.

Create and Automate Interactive PDF Forms with WPS Office
WPS Office provides robust PDF editing tools, allowing you to create interactive forms, add text fields, insert checkboxes, and manage form properties with ease.
- 1. Open the PDF in WPS Office: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab on the top ribbon.
- 2. Add Form Fields: Use the toolbar to insert 'Text Fields' and 'Checkboxes' onto your document page.
- 3. Configure Properties: Double-click any form field to open its properties, where you can set field names, appearance, and interactive behaviors.

Frequently Asked Questions
Why is my JavaScript validation not triggering in the PDF?
Ensure that you have placed the script in the 'Validate' or 'Calculate' tab of the text field properties, rather than as a document-level script. Additionally, check that JavaScript execution is enabled in your PDF viewer's security preferences.
How do I find the exact field name of my checkboxes?
Enter form editing mode and double-click or right-click the checkbox to open its properties. The 'Name' field under the 'General' tab contains the exact string you must use in your JavaScript code.
Can I use this method to check letters instead of numbers?
Yes. You can use the exact same 'event.value.slice(0, 1)' logic to extract the first letter. Simply update the cases in your switch statement to check for letters like 'A', 'B', or 'C' instead of digits.




