PDF Editing Issues

Validate an Acrobat Form Field for a Digit and Optional H

Maira MehtabMaira Mehtab Nov 19, 2026 869 views

Question details

Validate a PDF form field so it accepts one digit from 0 through 4, optionally followed by h.

Validate an Acrobat Form Field for a Digit and Optional H
Product
Adobe Acrobat
Device & OS
not provided
Scenario
A form author wants to restrict a text field to values such as 2 or 2h using Acrobat JavaScript.
Observed behavior
The field should accept a digit from 0 to 4 with an optional lowercase h and reject other nonempty values.
Before you start

Open the PDF in a version of Adobe Acrobat that can edit form fields, and identify the text field you want to validate. Decide whether the optional letter should be lowercase, uppercase, or either.

Solution 1Recommended

Add a custom validation script to the Acrobat text field

Use a regular expression to accept digits 0–4 with an optional lowercase h.

1
Open the form editor

Open the PDF in Acrobat and select All tools > Prepare a form, or Tools > Prepare Form, depending on your version.

2
Open field properties

Double-click the text field and open its Properties dialog. On the Options tab, you can set the character limit to 2.

3
Add the validation script

On the Validate tab, select Run custom validation script and click Edit. Enter: var value = event.value; var valid = /^[0-4]h?$/.test(value); if (value && !valid) { app.alert("Please enter a valid value."); event.rc = false; }

4
Save and test the field

Click OK to close the script editor, close Properties, and save the PDF. Test values such as 0, 4h, 5, and 2x in the field.

Add a custom validation script to the Acrobat text field
Empty values and uppercase H: The script allows an empty field. To accept uppercase H instead of lowercase h, use /^[0-4]H?$/. To accept either case, use /^[0-4][hH]?$/.
Free Microsoft Office alternative

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 Office documents. Its format compatibility makes it easy to bring existing files into your workflow; use Acrobat to configure the JavaScript validation described above.

Open and read PDF forms alongside your other documentsCompatible with Microsoft Word, Excel, and PowerPoint formatsFamiliar interface for an easier transition from Microsoft OfficeFree, lightweight tools for everyday document work
microsoft office alternative - wps office

Frequently Asked Questions

Which values does the validation script accept?

It accepts a single digit from 0 through 4, optionally followed by lowercase h, such as 3 or 3h. It also allows an empty field.

How can I accept both uppercase and lowercase H?

In the field's Validate > Run custom validation script editor, replace the regular expression with /^[0-4][hH]?$/. This accepts either h or H after the digit.

How do I prevent the field from being left blank?

Open the field's Properties dialog and select Required on the General tab. The validation script itself allows an empty value.

Why set a two-character limit if the script already validates the value?

The two-character limit in Properties > Options prevents longer entries, while the validation script checks that the characters have the correct form. The limit alone does not reject values such as 5h.