How to Validate and Format Time Entries in Acrobat PDF Forms
Question details
The user needs to validate raw digit inputs in a PDF form field and automatically format them into a proper hours and minutes (HH:MM) display.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating or modifying an interactive PDF form where users must input precise time values without manually typing the colon separator.
- Observed behavior
- Without validation, fields accept invalid times (e.g., 2400 or 578). The goal is to enforce valid 24-hour ranges (00-23 for hours, 00-59 for minutes) and format digits like 535 into 5:35 automatically.
Ensure you have a PDF editor that supports advanced JavaScript properties for interactive form fields, such as Adobe Acrobat Pro.
Use Custom JavaScript for Validation and Formatting
Apply a custom keystroke and validation script in the text field properties to automatically insert a colon and reject invalid time ranges.
You can enforce time formatting rules by adding a small JavaScript code snippet to your text field's validation properties. The script intercepts user input, checks the string length to determine where the colon goes, and validates the numeric range.
Open your PDF document in Form Editing mode. Right-click the specific text field where users will enter the time, and select Properties.
In the Text Field Properties dialog box, select the Validate tab at the top. Choose the 'Run custom validation script' option.
Click the Edit button and input the script to format the digits. Use code to check if the length is 3 or 4: var e = event.value; if (e.length == 3) event.value = e[0] + ':' + e[1] + e[2]; else if (e.length == 4) event.value = e[0] + e[1] + ':' + e[2] + e[3];
Below the formatting lines, add logic to split the formatted value and check the numbers: var y = event.value.split(':'); if (y[0] > 23 || y[1] > 59) { app.alert('Please enter a valid time.', 3); event.value = ''; }
Save and close the properties window. Exit editing mode and type test values (like 100, 535, 2359, 2400) to ensure the script properly accepts valid times and alerts on invalid ones.

Create and Edit PDF Forms Easily with WPS Office
WPS Office offers robust PDF editing capabilities, allowing you to seamlessly create, manage, and fill interactive PDF forms. With its user-friendly interface, you can effortlessly handle standard PDF inputs and formatting tasks.
- 1. Open your PDF: Launch WPS Office and open your existing PDF document or create a new one from scratch.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all available interactive form elements.
- 3. Insert Text Fields: Click on 'Text Field' and drag it onto your document layout where data input is needed.
- 4. Customize Properties: Double-click the newly created field to adjust its appearance, default values, and data formatting parameters.

Frequently Asked Questions
Why is my time validation script rejecting all entries?
Ensure your script correctly captures the event value and formats the string before splitting it. If the string format does not match the split character (such as missing a colon), the validation array will fail the logic condition.
Can I format time as a 12-hour AM/PM format instead of 24-hour?
Yes, you can modify the JavaScript to convert hours greater than 12 by subtracting 12 and appending 'PM'. However, this requires additional conditional statements in your custom formatting script.
Does this JavaScript validation work on all PDF readers?
Advanced JavaScript validation works best on fully-featured PDF editors like Adobe Acrobat and robust tools like WPS Office. Simple web browser PDF viewers may not execute custom validation scripts correctly, resulting in unformatted raw data.




