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.


