PDF Editing Issues

Trim and Capitalize PDF Form Text with JavaScript in Acrobat

Tauseeq MagsiTauseeq Magsi Nov 19, 2026 869 views

Question details

Automatically remove leading and trailing spaces, lowercase text, and capitalize the first letter of a PDF form name field.

Trim and Capitalize PDF Form Text with JavaScript in Acrobat
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the form editor

In Adobe Acrobat Pro, open the PDF and select All tools > Prepare a form.

2
Open field properties

Double-click the first-name or last-name text field to open its Properties, then select the Validate tab.

3
Add the script

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

4
Save and test

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.

Add a validation script to the PDF name field in Acrobat
Capitalization limits: The script capitalizes only the first character of the entire field. It does not preserve internal capitals in names such as McDonald or capitalize each part of a compound name.
Edit PDF names with WPS Office

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. 1. Open the PDF: Open WPS Office, select Open, and choose the fillable PDF.
  2. 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. 3. Save the form: Select Save or Save As, then reopen the PDF to check the corrected field.
Manually correct spacing and capitalization in PDF form fieldsOpen and edit Microsoft Office formats, including DOCX and XLSXUse a familiar interface for PDF and document tasks
microsoft office alternative - wps office

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.