How to Require Exactly Three Digits in a PDF Form Field Using JavaScript
Question details
The user needs a method to force a PDF form field to accept exactly three digits, displaying a custom alert message if the input is shorter or longer.

- Product
- PDF Form Editor
- Device & OS
- not provided
- Scenario
- Setting up data validation for a PDF text field where users are expected to enter a precise 3-digit numeric code.
- Observed behavior
- The current util.printx mask only triggers an alert when more than three digits are entered, but it fails to restrict or warn the user when fewer than three digits are submitted.
Ensure you have your PDF document open in form editing mode and have permissions to edit form fields and execute JavaScript validations.
Use Custom JavaScript Length Validation
Add a custom validation script to the text field to check the exact length of the user's input.
While formatting masks like util.printx are good for visual display, they do not inherently enforce a strict minimum character count. To force the user to enter exactly three characters and show a custom error message otherwise, a custom validation script is required.
Right-click the target text field in your PDF form editor and select 'Properties' from the context menu.
In the Text Field Properties dialog box, click on the 'Validate' tab at the top.
Select the radio button labeled 'Run custom validation script' and click the 'Edit...' button.
Input the following JavaScript code: if (event.value && event.value.length != 3) { app.alert('Please enter exactly three digits.'); event.rc = false; }
Click 'OK' to save the script, close the properties window, exit form editing mode, and test the field by entering one or two digits.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office features a robust PDF editor that allows you to easily design interactive forms, add text fields, insert checkboxes, and enforce data validation rules without hassle.
- 1. Open Your PDF: Launch WPS Office and open the PDF document you want to add form fields to.
- 2. Access the Form Tools: Navigate to the 'Form' tab located on the top navigation ribbon.
- 3. Insert Form Elements: Click on 'Text Field' or other form controls and draw the field directly onto the document.
- 4. Configure Properties: Double-click the newly created field to open its properties, where you can configure formatting and validation settings.

Frequently Asked Questions
Why doesn't the util.printx method work for minimum length validation?
The util.printx function is primarily designed as a display mask to format data structurally (like phone numbers or dates). While it drops extra characters, it doesn't natively enforce a minimum length restriction or trigger error alerts for missing characters.
Can I strictly limit the input to only numbers as well as a 3-character length?
Yes. To ensure the input contains exactly three numeric digits, you can use a regular expression in your custom validation script: if (event.value && !/^\d{3}$/.test(event.value)) { app.alert('Must be exactly 3 digits.'); event.rc = false; }
Where do I add JavaScript code in a standard PDF field?
JavaScript for field validation should be added in the 'Validate' tab of the Field Properties window by selecting 'Run custom validation script'.
How do I test my PDF JavaScript form fields?
You need to exit the form preparation/editing mode to interact with the fields as a standard user. Enter test data and click outside the text box or press Enter to trigger the validation script.




