PDF Editing Issues

How to Extract a Year or Number from a PDF Form Field Using JavaScript

Natalie TaylorNatalie Taylor Nov 5, 2026 869 views

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.

Extract a Year or Number from a PDF Form Field with JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Field Properties

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'.

2
Navigate to the Validation Tab

In the Text Field Properties dialog box, click on the 'Validate' tab to configure the actions triggered after data entry.

3
Add the Custom Script

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).

4
Save and Test

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.

Use a Custom JavaScript Validation Script
Handling Punctuation: If the user input is likely to contain extra spaces or trailing punctuation marks, using a Regular Expression (RegEx) instead of a simple space split provides more reliable text extraction.
Advanced PDF Form Editing

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. 1. Open your Document in WPS PDF: Launch WPS Office and open the PDF document you want to turn into an interactive form.
  2. 2. Access the Form Tools: Navigate to the 'Form' tab located on the top ribbon to reveal all interactive field options.
  3. 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. 4. Customize Properties: Set the field names, adjust formatting, and configure any necessary calculation or validation rules before saving your PDF.
Easily insert, edit, and manage interactive text fields, checkboxes, and drop-down menus.Fully compatible with Adobe Acrobat PDF formats, form fields, and Microsoft Office document layouts.Seamlessly convert and edit Microsoft Word, Excel, and PowerPoint documents as standard PDFs.Lightweight, fast, and accessible across Windows, Mac, iOS, and Android platforms.
QA img-9

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();`.