Validate an Acrobat Form Field for a Digit and Optional H
Question details
Validate a PDF form field so it accepts one digit from 0 through 4, optionally followed by 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.
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.
Add a custom validation script to the Acrobat text field
Use a regular expression to accept digits 0–4 with an optional lowercase h.
Open the PDF in Acrobat and select All tools > Prepare a form, or Tools > Prepare Form, depending on your version.
Double-click the text field and open its Properties dialog. On the Options tab, you can set the character limit to 2.
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; }
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.

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.

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.




