Ensuring phone numbers submitted through Contact Form 7 are valid helps filter out spam and improves lead quality. We will explore five ways to implement validation with code examples, examine the shortcomings of common techniques, and see how Abstract API overcomes these issues.
How to Implement Phone Number Validation in Contact Form 7
Here are three methods to add phone number validation to your forms. Each approach uses different techniques, from built-in features to custom code, to ensure number accuracy.
Contact Form 7’s Built-in Telephone Tag
Contact Form 7 includes `tel` and `tel*` tags for phone number fields. These tags use a built-in function, `wpcf7_is_tel()`, for server-side validation. The function's regular expression, `%^[+]?[0-9()/ -]*$%`, permits an optional plus sign followed by digits, spaces, parentheses, or dashes.
To implement this, you just add a required telephone field to your form. HTML5 performs the initial validation for attributes like `required`, `minlength`, and `maxlength`, while Contact Form 7 runs its own check upon submission. You can find more details on the WordPress Stack Exchange.
[tel* phone minlength:10 maxlength:15 placeholder "Phone"]
Server-side Override with a Filter
You can override the default validation logic with the `wpcf7_is_tel` filter. This requires you to add a custom function to your theme’s `functions.php` file or as a small must-use plugin.
The function uses `preg_match` to test the phone number against a more specific regular expression. Because this hooks into an internal filter, the new rule applies to both browser-side AJAX validation and the final server-side check without any form template changes. This Stack Overflow thread provides a useful example.
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
This method uses a plugin like Masks Form Fields to control user input on the front end. After you install the plugin, you add a specific class, such as `phone`, to your telephone field within the Contact Form 7 editor.
The plugin then applies a JavaScript input mask, which forces the user to enter numbers in a predefined format. This technique runs before Contact Form 7’s own validation and complements server-side checks by ensuring the submitted data already matches the desired structure.
[tel* phone class:phone placeholder "(000) 000-0000"]
Challenges of Phone Number Validation
While the methods above offer some control, they introduce significant validation challenges. These limitations often lead to false positives or the rejection of legitimate international phone numbers.
- A single regular expression cannot account for all international number formats. The built-in `tel` tag is too permissive, while custom filters require complex logic to avoid false positives or block valid numbers from different countries.
- Contact Form 7 removes the HTML pattern attribute. This action forces reliance on server-side hooks for validation, which adds latency to the user experience and increases the maintenance burden for developers who must handle every edge case.
- Core plugin updates can silently override custom validation rules. A past update hard-coded a minimum digit length, which broke forms that used custom filters for countries with shorter local phone numbers, making behavior unpredictable.
- Front-end input masks depend entirely on JavaScript. Users can disable it in their browser to bypass the format restriction. The fixed format also creates problems for international users whose numbers do not match the predefined structure.
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 in Contact Form 7
Abstract API addresses the core weaknesses of traditional validation methods through live lookups against carrier data.
- Contact Form 7’s native validator uses a simple regex that only checks for digits and a few symbols. It cannot confirm if a number is real, active, or reachable, so throw-away or mistyped numbers still pass.
- Custom regex or JavaScript masks are brittle, hard to internationalize, and remain blind to carrier data. They cannot detect line type, risk, or disposable number services.
- The Abstract API performs a live lookup against carrier data in more than 190 countries. It returns validity, risk score, line type, and carrier details in a single call. This process closes the core gaps, requires no regex maintenance, and adds accurate international support to reject high-risk numbers.
How to Add Abstract API to Your Dev Environment
Once you understand Abstract’s capabilities, you can add its phone number validation API to your project with ease.
- First, sign up at Abstract API, create a Phone Validation project, and copy your API key.
- Ensure Contact Form 7 is active in WordPress, then open your theme’s functions.php file.
- Add a required telephone field to your form, like this: [tel* user_phone].
- Insert the PHP code below into your functions.php file.
- Finally, deploy and test the changes. You can optionally cache successful numbers to reduce API calls.
- You can also tune rejection rules, such as the risk score, without any change to the form markup.
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;
}
Sample Phone Number Validation Implementation
The PHP code above hooks into Contact Form 7’s validation process. It sends the user-submitted phone number to Abstract API. The function then checks the API response to decide if the number is valid. If the `valid` field is false or the `risk_score` exceeds 0.3, the form shows an error. Otherwise, submission proceeds.
Here is a sample response from the API:
{
"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"
}
In this response, `valid: true` confirms the number exists and works. The low `risk_score` of 0.1 suggests it is not a disposable or fraudulent number. You can also use fields like `carrier` and `line_type` to create more advanced validation rules, such as a block on VoIP numbers.
Final Thoughts
Traditional phone validation methods rely on simple pattern checks that cannot detect fake or inactive numbers. They also fail to support international formats properly. Abstract API overcomes these limits with live carrier data to confirm a number is real, active, and low-risk. For reliable validation, create a free Abstract API account and get your 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