Trim and Capitalize PDF Form Text with JavaScript in Acrobat
Question details
Automatically remove leading and trailing spaces, lowercase text, and capitalize the first letter of a PDF form name field.

- Product
- PDF forms with JavaScript, such as Adobe Acrobat Pro
- Device & OS
- not provided
- Scenario
- A first-name or last-name form field needs consistent capitalization when a user enters text.
- Observed behavior
- The desired field value has no leading or trailing spaces, starts with an uppercase letter, and uses lowercase for the remaining characters.
Open the fillable PDF in a PDF editor that supports field-level JavaScript, such as Adobe Acrobat Pro. Check that the name field is a text field and that JavaScript is enabled.
Add a validation script to the PDF name field in Acrobat
Use this method to format a name automatically when the field value is validated.
In Adobe Acrobat Pro, open the PDF and select All tools > Prepare a form.
Double-click the first-name or last-name text field to open its Properties, then select the Validate tab.
Select Run custom validation script, click Edit, and enter: var value = event.value.trim().toLowerCase(); event.value = value.substring(0, 1).toUpperCase() + value.substring(1);
Click OK to close the script editor and field properties, then save the PDF. Enter a name with extra spaces and mixed capitalization, and move to another field to test it.

Correct PDF form names manually in WPS Office
Use WPS Office to fill a PDF name field and correct its spacing and capitalization manually. This is a one-time alternative, not a replacement for automatic JavaScript validation.
- 1. Open the PDF: Open WPS Office, select Open, and choose the fillable PDF.
- 2. Correct the name: Click the first-name or last-name field. Remove spaces at the beginning and end, capitalize the first letter, and lowercase the remaining letters.
- 3. Save the form: Select Save or Save As, then reopen the PDF to check the corrected field.

Frequently Asked Questions
Where should I put the JavaScript in Acrobat?
In Acrobat Pro, select All tools > Prepare a form, open the text field's Properties, and use Validate > Run custom validation script > Edit.
Does the script remove spaces between words?
No. JavaScript's trim() removes whitespace only from the beginning and end of the value; spaces inside the name remain.
Will this capitalize both parts of a compound last name?
No. It capitalizes only the first character of the whole field and lowercases the rest. Compound names may need a different capitalization rule.
Why does the script not run in another PDF viewer?
The viewer may not support PDF form JavaScript or may have it disabled. Test the form in Acrobat with JavaScript enabled, and check the other viewer's capabilities.




