How to Validate Email or Phone Numbers in a PDF Form
Question details
The user needs to dynamically validate a text field in a PDF form as either an email address or a phone number, depending on whether an 'Email' or 'Phone' radio button is selected.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where the validation logic for a contact info field changes based on the user's preferred contact method selection.
- Observed behavior
- The form requires applying an email validation script without a fixed mask when 'Email' is chosen, and applying a specific phone number pattern (e.g., (999) 999-9999) when 'Phone' is chosen.
Ensure you are using a PDF editor with advanced form editing capabilities and have basic knowledge of JavaScript, as dynamic conditional validation requires custom scripting.
Apply Conditional JavaScript Validation Based on Radio Button Selection
Use a custom validation script on the text field that checks the selected value of the radio button group to determine whether to run an email validation Regex or apply a phone number format.
By leveraging JavaScript within your PDF form, you can dynamically read the state of a radio button and apply the appropriate validation rules to a single text input field.
Open your PDF form in edit mode. Double-click your radio button group to open its 'Properties'. Ensure the group has a specific name (e.g., 'ContactMethod') and check the 'Options' tab to confirm the export values are clearly defined (e.g., 'Email' and 'Phone').
Right-click the target text field where users will type their information and select 'Properties' from the context menu.
Navigate to the 'Validate' tab and choose the 'Run custom validation script' option. Click the 'Edit' button.
Enter a JavaScript block that evaluates `this.getField('ContactMethod').value`. Create an `if` statement: if the value equals 'Email', use a Regular Expression to check the input string's email format. If the value equals 'Phone', apply standard phone number formatting logic. Click 'OK' to save.
Close the form editing mode and switch to the 'Hand' tool or 'Preview' mode. Select the 'Email' radio button and attempt to enter text to verify the email validation triggers, then switch to 'Phone' to test the numerical formatting.

Build and Validate Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to easily insert interactive form fields, customize properties, and apply JavaScript validation, ensuring your data collection is accurate and professional.
- 1. Open PDF Form Editor: Open your PDF document in WPS Office and navigate to the 'Form' tab on the top ribbon.
- 2. Insert Form Controls: Select 'Text Field' or 'Radio Button' from the toolbar to insert the required interactive elements into your document.
- 3. Customize Field Properties: Double-click the inserted field to open the Properties dialog, where you can define names, export values, and implement custom validation scripts.

Frequently Asked Questions
Why shouldn't I use a format mask for email addresses in PDF forms?
Email addresses have varying lengths and structures (e.g., different domain lengths, special characters). A fixed format mask (like those used for dates or phone numbers) will restrict legitimate email inputs and break the form. Standard Regular Expression (Regex) validation is recommended instead.
Can I automatically format a phone number as it is typed?
Yes. In the text field properties under the Format tab, you can select 'Special' and choose 'Phone Number', or use a custom keystroke script to automatically format the user's input into a desired pattern like (999) 999-9999 on the fly.
Why is my custom JavaScript validation not running in the PDF?
Ensure that JavaScript is enabled in your PDF viewer's preferences. Additionally, double-check that the field names referenced in your custom script (e.g., using `this.getField('FieldName')`) exactly match the actual names of your form fields, as JavaScript is strictly case-sensitive.




