How to Validate Multiple Cost Code Formats in Mobile PDF Forms
Question details
The user needs a dynamic PDF timesheet to accept two specific cost code formats (9-99-999 or 99-99-999) without leading zeros, functioning seamlessly across various mobile devices.

- Product
- Device & OS
- iPhone, iPad, Android
- Scenario
- Creating a dynamic PDF timesheet for field staff that requires specific formatting rules for cost code entry fields.
- Observed behavior
- Standard Acrobat format masks only support one fixed format, failing to accommodate both required structural variations and unable to block leading zeros.
Before applying custom scripts, ensure your preferred PDF editor supports JavaScript for form validation, and note that script execution can vary significantly depending on the mobile PDF reader app being used.
Use Custom JavaScript Regular Expressions for Validation
Since a single format mask cannot support multiple structures, you must use a custom JavaScript validation script with regular expressions to enforce the two cost code formats and reject leading zeros.
A custom validation script evaluates the user's input against predefined regular expressions. It checks if the entry matches either the 9-99-999 or 99-99-999 format. Additionally, it applies a specific check to ensure the first digit is not a zero when the two-digit prefix is used.
Right-click the designated cost code text field in your PDF form editor and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Validate' tab to configure the input rules for the field.
Select the option to 'Run custom validation script' and click the 'Edit' button to open the JavaScript editor.
Paste a JavaScript snippet that tests the event.value against the required regular expressions (e.g., /^\d{2}-\d{2}-\d{3}$/ and /^\d{1}-\d{2}-\d{3}$/) while explicitly setting event.rc to false if the value begins with a zero. Save your changes.
Because JavaScript support varies across mobile applications, transfer the updated PDF to an iPhone, iPad, and Android device. Test the form fields using the exact PDF reader applications your field staff will utilize.

Manage Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive suite of PDF tools that allow you to seamlessly view, fill, and manage interactive forms on both desktop and mobile platforms.
- 1. Open the PDF File: Launch WPS Office on your desktop or mobile device and open your interactive PDF timesheet.
- 2. Fill the Form Fields: Tap or click on the cost code fields to input your data; WPS PDF accurately renders interactive text fields.
- 3. Save and Share: Once the timesheet is completed, save the document and use the built-in share feature to send it to your team directly.

Frequently Asked Questions
Why isn't my PDF form validation script working on mobile devices?
Many default or third-party mobile PDF readers offer limited or no support for JavaScript execution. To ensure your custom validation scripts work, your users must open the form in a fully compliant PDF application designed to handle advanced interactive elements.
Can I use standard Acrobat format masks for two different formats?
No, standard format masks in PDF editors only allow you to define a single, fixed input structure. When a field requires multiple valid formats, such as varying prefix lengths, custom JavaScript is the only way to enforce the rules.
How do I prevent users from entering a leading zero in a PDF text field?
You can block leading zeros by adding a condition in your custom JavaScript validation script. For example, using a regular expression like /^0/.test(event.value) allows the script to detect a leading zero and reject the input by triggering an error alert.




