How to Run a Script While Entering a PDF Form Field Value
Question details
The user wants to execute a JavaScript action automatically as they type into a PDF form field, such as shifting focus after a specific number of characters.

- Product
- Adobe Acrobat / PDF
- Device & OS
- not provided
- Scenario
- Filling out interactive PDF forms where automated cursor movement or real-time text validation is needed.
- Observed behavior
- The user needs a script to trigger mid-entry rather than waiting until the field loses focus or is manually submitted.
Ensure you are using a PDF editor that supports JavaScript execution for form fields and that the target text fields have already been created.
Use the Keystroke Event to Trigger the Script
Utilize the custom Keystroke script property of the text field to monitor input length and execute actions dynamically as the user types.
By assigning a JavaScript to the Keystroke event, the PDF viewer evaluates the script every time a key is pressed within that specific field, allowing for real-time field formatting or focus changes.
Right-click the text field you want to modify in your PDF editor and select 'Properties' from the context menu.
In the Text Field Properties dialog box, click on the 'Format' tab to access custom formatting options.
Choose 'Custom' from the Select Format Category dropdown list to enable custom scripting.
In the Custom Keystroke Script box, click 'Edit' and enter your script. To move focus after 6 characters, enter: if (AFMergeChange(event).length == 6) this.getField('target_field_name').setFocus();
Click 'OK', close the properties dialog, and test your form field by typing into it to ensure the focus shifts automatically.

Create and Fill PDF Forms Easily with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to create, edit, and seamlessly fill out interactive PDF forms without the need for expensive software.
- 1. Open WPS Office: Launch WPS Office and open your PDF document using the built-in PDF editor.
- 2. Access Form Features: Navigate to the 'Form' tab on the top ribbon to view all interactive form field tools.
- 3. Edit or Fill Fields: Click on 'Text Field' to add new input areas, or simply click into existing fields to start typing your data.
- 4. Save Your Document: Once your form is configured or filled, click 'Save' or 'Export to PDF' to preserve your changes securely.

Frequently Asked Questions
What is a Keystroke event in PDF forms?
A Keystroke event is a trigger that fires every time a user types a character into a form field, allowing embedded scripts to validate or react to input in real-time.
Can I use JavaScript to auto-tab to the next field in a PDF?
Yes, by writing a custom Keystroke script that checks the length of the input and uses the setFocus() method to jump to the target field once the condition is met.
Why isn't my PDF JavaScript executing when I type?
Ensure that JavaScript is enabled in your PDF viewer's preferences, and check that your script is placed in the Keystroke event tab rather than the On Blur or On Focus events.
Is JavaScript strictly required for moving to the next field automatically?
Yes, if you want the focus to move automatically after a specific number of characters, a custom JavaScript tied to the field's Keystroke event is the standard method in PDF forms.




