Validate an Acrobat Form Field for Letters or Numbers Only
Question details
The user needs an Acrobat form field to accept either letters or numbers, but not a mixture of both, without text affecting calculations.

- Product
- Adobe Acrobat form fields
- Device & OS
- not provided
- Scenario
- A PDF form field can contain a text note or a numeric value and is referenced by a calculation.
- Observed behavior
- The desired form accepts one type of entry at a time and excludes text entries from numeric calculations.
Open the PDF in Acrobat and select Prepare a form. Identify the input field and any calculated fields that reference it, and check that JavaScript is enabled for testing.
Add a custom validation script and adjust the calculation
Use this approach to reject mixed entries when the field is committed and prevent text from being included in a numeric sum.
The validation pattern below permits an empty field, ASCII letters only, or digits only. It rejects spaces, punctuation, decimal points, signs, and entries that combine letters and digits.
The calculation example is for a sum. Adapt the existing calculation rather than replacing it if it also uses other fields or performs a different operation.
In Acrobat, select Prepare a form, then double-click the input field to open its properties.
On the Validate tab, select Run custom validation script and click Edit. Enter: var s = String(event.value); if (s !== "" && !/^(?:[A-Za-z]+|[0-9]+)$/.test(s)) { app.alert("Please enter letters only or digits only."); event.rc = false; }
Open the calculated field's properties, select the Calculate tab, and edit its custom calculation script. In the part that adds the input field to a sum, use the following pattern, replacing InputField with the exact field name and using the script's existing total variable: var s = String(this.getField("InputField").value); if (/^[0-9]+$/.test(s)) { total += Number(s); } Assign the final total to event.value after processing the fields.
Use Preview in Prepare a form to test a blank value, letters, digits, and a mixed entry such as ABC123. Confirm that mixed entries are rejected and that a text entry does not change the calculated sum.

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Microsoft Office files. Its format compatibility helps you move existing documents into your workflow, but it does not replace Acrobat's custom form JavaScript validation.

Frequently Asked Questions
Where do I add the validation script in Acrobat?
Select Prepare a form, double-click the input field, and open the Validate tab. Select Run custom validation script and click Edit.
Will the script allow a blank field?
Yes. The example explicitly allows an empty value. Use the field's General properties to mark it as required if users must complete it.
Can users enter text notes with spaces?
Not with the supplied pattern: it accepts ASCII letters only or digits only. The pattern must be changed if notes need spaces or punctuation.
Why must I change the calculation script too?
Validation allows letter-only entries, but a calculation that references the field still needs to recognize and skip them. In the calculated field's Calculate tab, check for digits before converting and adding the value.




