How to Replace Spaces, Commas, and Dashes with Underscores in Acrobat PDF Forms using JavaScript
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.

- 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.
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.
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.
Open your PDF form and enter the 'Prepare Form' or form editing mode.
Right-click the target text field you want to format and select 'Properties' from the context menu.
In the Text Field Properties window, click on the 'Validate' tab.
Select the 'Run custom validation script' option and click the 'Edit' button. Enter the following code: event.value = event.value.replace(/[\s-,]/g, "_");
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.

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. Install WPS Office: Download and install WPS Office Free on your device.
- 2. Open your PDF: Launch the WPS PDF viewer and open your interactive PDF form document.
- 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. Save the Document: Once completed, save or export your filled PDF directly within the application.

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.




