PDF Editing Issues

How to Replace Spaces, Commas, and Dashes with Underscores in Acrobat PDF Forms using JavaScript

Steven ParkerSteven Parker Nov 3, 2026 869 views

Question details

The user needs to automatically convert spaces, commas, and hyphens into underscores within an interactive PDF form field using a custom validation script.

Replace Spaces, Commas, and Dashes with Underscores in Acrobat PDF Forms via JavaScript
Product
Adobe Acrobat / PDF Forms
Device & OS
not provided
Scenario
Standardizing data entry formats in interactive PDF forms by normalizing separator characters.
Observed behavior
The user wants input text like '2019-RM-15' or 'Test, Name' to instantly reformat to '2019_RM_15' and 'Test__Name' upon input validation.
Before you start

Ensure you are in the form editing mode of your PDF application and have access to the properties menu of the specific text field you wish to validate.

Solution 1Recommended

Use Custom JavaScript Regex Validation in Field Properties

Apply a short JavaScript code snippet using a regular expression to format the field value automatically whenever data is entered.

By utilizing Acrobat's built-in JavaScript engine, you can intercept the user's input before it is finalized. A simple Regular Expression (Regex) can globally search the input string for any targeted characters and replace them in real-time.

1
Enter Form Editing Mode

Open your PDF form and enter the 'Prepare Form' or form editing mode.

2
Open Field Properties

Right-click the target text field you want to format and select 'Properties' from the context menu.

3
Navigate to the Validate Tab

In the Text Field Properties window, click on the 'Validate' tab.

4
Add the Custom Script

Select the 'Run custom validation script' option and click the 'Edit' button. Enter the following code: event.value = event.value.replace(/[\s-,]/g, "_");

5
Save and Test

Click 'OK' to save the script, then close the properties window. Test the form field by typing text with spaces, hyphens, or commas and clicking outside the field to see the characters convert automatically.

Use Custom JavaScript Regex Validation in Field Properties
Regex Explanation: The expression /[\s-,]/g targets all whitespace (\s), hyphens (-), and commas (,) globally (g) across the entire input string. Test the script with empty values to ensure no errors occur.
Powerful PDF Form Management

Edit and Fill Interactive PDF Forms with WPS Office

WPS Office provides robust PDF tools, allowing you to easily view, edit, and manage interactive PDF forms without needing heavy or expensive software. Streamline your document workflow efficiently.

  1. 1. Install WPS Office: Download and install WPS Office Free on your device.
  2. 2. Open your PDF: Launch the WPS PDF viewer and open your interactive PDF form document.
  3. 3. Fill the Form: Click on the designated form fields to enter your data or navigate to the 'Form' tab for more editing tools.
  4. 4. Save the Document: Once completed, save or export your filled PDF directly within the application.
Seamlessly open and interact with standard PDF forms.Highly compatible with Microsoft Office formats (Word, Excel, PowerPoint) and Adobe PDFs.Lightweight application that won't slow down your computer.Edit text, add images, and manage fields with a highly intuitive interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my JavaScript validation script not running in the PDF?

Ensure that JavaScript execution is enabled in your PDF viewer's preferences. Additionally, note that custom JavaScript validations are often not supported if the PDF is opened in a web browser's built-in viewer; always test in a dedicated PDF desktop application.

Can I modify the script to replace other special characters?

Yes, you can update the regex pattern inside the square brackets. For example, changing the regex to /[\s-,@#]/g will also replace the '@' and '#' symbols with underscores.

Does replacing spaces with underscores change the exported data format?

Yes, the custom validation modifies the actual stored value of the form field. When you export the form data to a CSV or database format, the underscores will be retained instead of the original user-input spaces or dashes.