PDF Editing Issues

How to Validate Email or Phone Numbers in a PDF Form

Anum MushtaqAnum Mushtaq Nov 5, 2026 869 views

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.

How to Validate Email or Phone Numbers in a PDF Form
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.
Before you start

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.

Solution 1Recommended

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.

1
Verify radio button group properties

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').

2
Access the text field properties

Right-click the target text field where users will type their information and select 'Properties' from the context menu.

3
Insert the custom validation script

Navigate to the 'Validate' tab and choose the 'Run custom validation script' option. Click the 'Edit' button.

4
Write the conditional logic

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.

5
Test the form

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.

Apply Conditional JavaScript Validation Based on Radio Button Selection
Avoid Email Input Masks: Unlike phone numbers, email addresses should never use a fixed format mask because domain structures and username lengths vary drastically. Always rely on Regex validation for emails.
Powerful PDF Form Editor

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. 1. Open PDF Form Editor: Open your PDF document in WPS Office and navigate to the 'Form' tab on the top ribbon.
  2. 2. Insert Form Controls: Select 'Text Field' or 'Radio Button' from the toolbar to insert the required interactive elements into your document.
  3. 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.
Easily add text fields, radio buttons, and checkboxes to any PDF document.Support for custom JavaScript to handle complex data validation like emails and phone numbers.Highly compatible with Adobe Acrobat PDF forms and standard PDF readers.Lightweight software with an intuitive, user-friendly interface.
microsoft office alternative - wps office

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.