Ensuring you capture valid phone numbers through Contact Form 7 helps you connect with leads and avoid bad data. We'll walk through four ways to implement phone number validation, providing working code for each. We'll also cover the pitfalls of these methods and show how Abstract API addresses them.
How to Implement Phone Number Validation in Contact Form 7
Here are three ways to add phone number validation to your forms. Each method uses a different approach, from built-in features to custom code for more precise control.
Contact Form 7’s Built-in Telephone Tag
Contact Form 7 includes “tel” and “tel*” tags that perform server-side validation. This check uses the `wpcf7_is_tel()` function, which validates against a permissive regular expression.
The pattern, “%^[+]?[0-9()/ -]*$%”, allows an optional plus sign followed by digits, spaces, parentheses, or dashes. This method requires minimal work. You just add a required “tel*” field to your form.
HTML5 performs the initial validation for attributes like “required”, “minlength”, and “maxlength”. Contact Form 7 then runs its own validation when the user submits the form.
[tel* phone minlength:10 maxlength:15 placeholder "Phone"]
A Server-Side Validation Override with a Filter
You can override the default validation logic on the server. This requires you to create a small mu-plugin or add a code snippet to your theme’s `functions.php` file.
The code hooks into the `wpcf7_is_tel` filter to replace the default validation rule. The new function uses `preg_match` with a more specific regular expression to check the phone number format.
Because this method uses an internal filter, the same validation rule executes for both the browser’s AJAX response and the final server-side check. No changes to your form template are necessary.
add_filter('wpcf7_is_tel','custom_tel',10,2);
function custom_tel($result,$tel){
$result = preg_match('/^\(?\+?([0-9]{1,4})?\)?[-\. ]?(\d{10})$/',$tel);
return $result;
}
Front-End Input Masking with a Plugin
A front-end mask guides user input into a specific format. You can achieve this with a plugin like Masks Form Fields. After you install the plugin, you add a class like “phone” to your telephone field.
The plugin uses jQuery Mask to apply the format, so users can only type characters that match the defined pattern. This method works well with server-side validation, as the mask cleans the input before Contact Form 7 processes it.
[tel* phone class:phone placeholder "(000) 000-0000"]
Common Pitfalls of Phone Number Validation
The methods discussed offer some control but come with significant drawbacks. These limitations can create unreliable validation, a poor user experience, and ongoing maintenance issues.
- A single regular expression cannot cover all international number formats. Contact Form 7’s built-in tel filter is too permissive and accepts long strings of numbers, which forces you to build complex logic or accept false positives.
- Contact Form 7 removes the HTML pattern attribute, so you lose native client-side validation. You must rely on server-side hooks for every edge case, which adds latency and increases the maintenance burden for your forms.
- Plugin updates can break your forms. A new version might hard-code a minimum length or override your custom filters without notice. This makes validation behavior unpredictable and can block legitimate users from specific countries.
- Front-end input masks depend on JavaScript, so users can bypass them if they disable it. The fixed format also creates problems for international users whose phone numbers do not match the mask’s specific pattern.
Validate Phone Numbers with Abstract API
Add phone number validation to your forms to ensure you only collect accurate contact data.
Get started for free
How Abstract API Handles Phone Number Validation
Abstract API addresses the core weaknesses of traditional validation methods through a live lookup against carrier data that confirms a number’s real-world status.
- Contact Form 7’s native validator relies on a simple pattern match that cannot confirm if a number is real or active, so incorrect numbers still pass.
- Custom code extensions are often brittle, struggle with international formats, and cannot detect carrier details, line type, or risk factors.
- The API queries carrier databases in over 190 countries to return a number’s validity, risk score, line type, and carrier, which eliminates regex maintenance and adds accurate international support.
How to Set Up Abstract API in Your Project
Once you know Abstract’s capabilities, you can add its phone number validation to your project with ease. For a WordPress site that uses Contact Form 7, the process involves a few direct steps.
- Sign up at Abstract, create a Phone Validation project, and copy your API key.
- Ensure Contact Form 7 is active in WordPress and open your theme’s functions.php file.
- Add a required telephone field, such as [tel* user_phone], to your form.
- Insert a PHP snippet to connect the form submission to the API endpoint.
- Deploy the changes and test the form with various phone numbers.
- Optionally, tune the rejection rules based on the API response, such as the risk score or line type.
Sample Implementation with Abstract API and Contact Form 7
The PHP code below integrates directly with Contact Form 7 via a filter hook. When a user submits a form, the function intercepts the phone number, removes any non-digit characters, and sends it to the Abstract API endpoint. It then checks the response.
If the API returns "valid" as false or the "risk_score" exceeds a set threshold, it invalidates the field and shows an error message. Otherwise, the form submission proceeds.
// functions.php
add_filter( 'wpcf7_validate_tel*', 'cf7_abstract_phone_validate', 10, 2 );
function cf7_abstract_phone_validate( $result, $tag ) {
$phone = isset($_POST[$tag->name]) ? preg_replace('/\D+/', '', $_POST[$tag->name]) : '';
if ( ! $phone ) { return $result; }
$endpoint = 'https://phonevalidation.abstractapi.com/v1/?api_key=YOUR_KEY&phone=' . $phone;
$res = wp_remote_get( $endpoint, [ 'timeout' => 3 ] );
if ( is_wp_error( $res ) ) { return $result; }
$data = json_decode( wp_remote_retrieve_body( $res ), true );
if ( empty( $data['valid'] ) || $data['risk_score'] > 0.3 ) {
$result->invalidate( $tag, 'Please enter a valid phone number.' );
}
return $result;
}
A successful API call returns a detailed JSON object. This data gives you precise control over validation.
{
"phone": "14154582468",
"valid": true,
"risk_score": 0.1,
"registered_location": "San Francisco",
"carrier": "Verizon USA",
"line_type": "Mobile",
"local_format": "4154582468",
"international_format": "+14154582468",
"country_prefix": "+1",
"country_code": "US",
"country_name": "United States of America"
}
Here, "valid" as "true" confirms the number exists, and a low "risk_score" suggests it is legitimate. You can also use fields like "line_type" or "country_code" to enforce custom rules, such as to block VoIP numbers or restrict submissions to certain countries.
Final Thoughts
Traditional validation methods often fail because they cannot confirm if a number is real, active, or high-risk. They depend on simple pattern checks that let incorrect data through. Abstract API performs a live lookup against carrier databases to provide definitive validation, which closes these gaps and protects your forms from bad data. To reliably validate user phone numbers, consider an account with Abstract API to get your free API key.
Validate Phone Numbers with Abstract API
Implement phone number validation now to stop receiving fake submissions and improve data quality.
Get started for free