Remove Leading and Trailing Spaces from Acrobat Form Fields
Question details
Remove whitespace from the beginning and end of text entered in a PDF form field without removing spaces within the text.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form needs to clean up names or other text when users finish entering a field value.
- Observed behavior
- Leading and trailing whitespace is removed while spaces between words remain intact.
Open the PDF in an Acrobat edition that lets you edit form fields, and make sure you can access the field's Properties dialog. The validation script runs when a field value is committed, so test it by changing the field and moving focus away.
Add a custom validation script to the Acrobat form field
Use the field's validation settings to trim whitespace from both ends of an entered value while preserving internal spaces.
Open the PDF in Adobe Acrobat and select Prepare a form from the tools menu.
Right-click the text field you want to clean up, then select Properties.
On the Validate tab, select Run custom validation script, click Edit, and enter: event.value = event.value.trim();
Confirm the script, close Properties, and save the PDF. Enter a value such as " Jane Doe " in the field, then click another field; the result should be "Jane Doe".

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative for everyday document work, with a familiar interface and compatibility with common Microsoft Office formats for easier migration. Its PDF tools can help with general PDF tasks, but they do not replace Acrobat's custom form validation script.

Frequently Asked Questions
Will trim() remove the space between a first and last name?
No. The script event.value = event.value.trim(); removes whitespace only from the beginning and end of the field value, leaving internal spaces intact.
Where should I put the script in Acrobat?
In Prepare a form, right-click the text field and choose Properties. On the Validate tab, select Run custom validation script and click Edit.
When does the validation script run?
It runs when the field value is committed, such as after a user changes the text and moves focus to another field.
Will this script automatically clean every existing field value?
No. The script applies to the field where it is added and runs when that field's value is validated. Existing values may need to be changed and committed before they are trimmed.




