How to Extract a Year or Number from a PDF Form Field Using JavaScript
Question details
Extracting a specific segment of text, such as a year or number, from a user-entered PDF form field and automatically pasting it into another field.

- Product
- WPS PDF
- Device & OS
- not provided
- Scenario
- Automating data entry in an interactive PDF form where part of a string (like a trailing date or year) needs to be separated and recorded in a distinct field.
- Observed behavior
- The user inputs a combined text string in one field, and the form needs a script to isolate the final segment and display it in a secondary destination field.
Ensure your PDF editor supports interactive form field properties and custom JavaScript validation. Note the exact, case-sensitive names of both your source and destination text fields.
Use a Custom JavaScript Validation Script
Apply a split and pop array method in the source field's custom validation properties to push the final segment of text to the target field.
This method involves triggering a script the moment the user finishes entering data into the source field. By splitting the string at every space, you can isolate the last word or number and inject it into a secondary field.
Enter the form editing mode of your PDF software. Right-click the source text field where users will type the original string and select 'Properties'.
In the Text Field Properties dialog box, click on the 'Validate' tab to configure the actions triggered after data entry.
Select the 'Run custom validation script' option and click 'Edit'. Enter the following script: `this.getField("Text2").value = event.value.split(" ").pop();` (Be sure to replace "Text2" with the exact name of your destination field).
Click 'OK' to save the script and close the properties window. Exit form editing mode and type a test phrase ending with a year into the source field; the year should automatically appear in your target field when you click away.

Create and Manage Interactive PDF Forms with WPS Office
WPS PDF provides comprehensive tools for building, filling, and managing interactive PDF forms. You can easily insert text fields, adjust form properties, and handle document calculations with an intuitive interface.
- 1. Open your Document in WPS PDF: Launch WPS Office and open the PDF document you want to turn into an interactive form.
- 2. Access the Form Tools: Navigate to the 'Form' tab located on the top ribbon to reveal all interactive field options.
- 3. Add or Edit Text Fields: Click on 'Text Field' and draw it on your document. Double-click any newly created or existing field to open its properties panel.
- 4. Customize Properties: Set the field names, adjust formatting, and configure any necessary calculation or validation rules before saving your PDF.

Frequently Asked Questions
How can I extract numbers using a Regular Expression instead of splitting by spaces?
To extract numbers regardless of their position in the string, use a RegEx match in your validation script. For example, `this.getField("Text2").value = event.value.match(/\d+/)[0];` will find and extract the first sequence of digits.
Why is my target form field remaining blank after typing?
This usually happens if the target field name in your script does not exactly match the actual field name in the PDF. Check for typos or capitalization errors, as field names are case-sensitive. Additionally, ensure your script is placed in the 'Validate' tab of the source field.
Can I extract the first word instead of the last one?
Yes. If you are using the split array method, you can replace the `pop()` function with `shift()`. Your script would look like this: `this.getField("Text2").value = event.value.split(" ").shift();`.




