Guides
Last updated
July 20, 2025

5 Ways to Validate Phone Numbers in Contact Form 7

Nicolas Rios
Nicolas Rios
Table of Contents:
ON THIS PAGE
Get your free
phone validation
 API key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

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 Handles Phone Number Validation

Abstract 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 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 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 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.

A successful request returns a detailed JSON object. This data gives you precise control over validation.

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 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 to get your free API key.

Frequently Asked Questions

Does Contact Form 7 validate phone numbers by default?

Contact Form 7 includes a built-in telephone tag ([tel* phone]) that uses the wpcf7_is_tel() function to check for digits, spaces, parentheses, and dashes. However, its regex is intentionally permissive and cannot confirm whether a number is real, active, or legitimately formatted for a given country.

How do I add stricter server-side phone validation to Contact Form 7?

You can override the default validation by adding a custom filter to your theme's functions.php using the wpcf7_is_tel hook and preg_match with a stricter regex pattern. This approach is more reliable than relying on the default tag, but a single regex still cannot handle all international number formats correctly.

What is the most reliable way to validate phone numbers in Contact Form 7?

Using an API like Abstract's Phone Validation API gives the most reliable results. By hooking into wpcf7_validate_tel* and calling the API on form submission, you get a live lookup against carrier databases in over 190 countries that returns a validity status, risk score, line type, and carrier, something no regex can replicate.

How do I integrate the Abstract Phone Validation API with Contact Form 7?

Add a filter for wpcf7_validate_tel* in functions.php that extracts the submitted phone number, sends it to https://phonevalidation.abstractapi.com/v1/ via wp_remote_get(), then calls $result->invalidate() if the response returns valid: false or a risk_score above your chosen threshold. The API returns JSON with fields including valid, risk_score, line_type, and carrier.

Can I use Contact Form 7 phone validation to block high-risk or VoIP numbers?

Yes. The Abstract response includes a risk_score field and a line_type field (such as "Mobile", "Landline", or "VOIP"). You can add conditional logic in your validation function to reject numbers whose risk score exceeds a threshold or whose line type does not match what your form requires.

Why might JavaScript-based phone input masking not be enough for Contact Form 7?

Input masking plugins guide users toward a specific format but rely on JavaScript running in the browser, which can be bypassed or disabled. They also cannot verify whether the number is active or registered to a real carrier. Server-side validation (especially with a live API lookup) is necessary to enforce true accuracy before form submission is accepted.

Validate Phone Numbers with Abstract API
Implement phone number validation now to stop receiving fake submissions and improve data quality.
Get started for free

Related Articles

Phone Validation
key now
Get your free
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required