How Abstract API Handles Phone Number Validation in Yup
Abstract API addresses the core weaknesses of traditional validation methods. It moves beyond simple syntax checks to provide semantic validation against real-time telecom data.
- It consults a real-time telecom dataset that covers more than 190 countries to verify numbers.
- It returns a boolean flag that confirms if a number is active and reachable.
- It provides useful metadata, such as the line type, carrier, and location details.
- It moves the complex validation logic out of the browser, which keeps the client bundle small.
How to Bring Abstract API to Your Dev Environment
Once you are familiar with Abstract's capabilities, to add its phone number validation API to your project is simple.
- Create a free account on Abstract to get your Phone Validation API key.
- Place the key in a .env file with the variable name ABSTRACT_API_KEY.
- Install the necessary packages to make HTTP requests and load environment variables.
Sample Phone Number Validation Implementation with Abstract API
The helper function below shows a complete validation flow. It first uses a Yup schema for a quick format check on the client side. Then, it sends the phone number to the Abstract API endpoint.
If the API response property "valid" is not "true", the function throws an error. Otherwise, it returns the full JSON object from the API, which contains rich metadata about the number.
Here is an example response for a valid number:
The "valid" field confirms the number is active. The "format" object provides normalized versions for display or storage. The "country", "location", "type", and "carrier" fields offer detailed context that helps with regional logic and route decisions.
Final Thoughts
Traditional validation methods only check syntax. They cannot confirm if a number is active or identify its line type, which leaves gaps for fraud and affects SMS deliverability.
Abstract API solves this with real-time data for semantic checks. Consider an account on Abstract API to get your free API key and reliably validate user phone numbers.
Frequently Asked Questions
What is phone number validation in Yup?
Phone number validation in Yup means adding a rule to a Yup schema that checks whether a submitted string matches a valid phone number format before the form is submitted. Yup offers several ways to do this: a regex pattern with .matches(), a custom .test() function backed by libphonenumber-js, or a third-party plugin like yup-phone. Each approach runs synchronously on the client side and catches formatting errors early.
Should I use a regex or libphonenumber-js with Yup?
Regex is simpler and adds no dependencies, making it a good fit when you only need to enforce a single format like E.164. libphonenumber-js is more reliable for international and national number formats because it uses Google's phone metadata and handles edge cases that a hand-written regex often misses. The trade-off is bundle size: libphonenumber-js adds roughly 145 kB, while a regex adds nothing. For forms that accept numbers from multiple countries, libphonenumber-js is the safer choice.
What does the yup-phone plugin add over plain Yup?
The yup-phone plugin extends Yup's string type with a .phone() method, so you can write Yup.string().phone('US', true) instead of wiring up a custom .test() manually. It supports strict and loose validation modes and accepts an optional country code for region-specific rules. One known issue is that yup-phone can behave unexpectedly with nullable fields, so you may need to chain .nullable() carefully or switch to yup-phone-lite, which uses libphonenumber-js and has a smaller footprint.
How do I validate a phone number conditionally in Yup?
Use Yup's .when() method to tie phone validation to another field in the same schema. For example, you can require a phone number only when a user selects SMS as their contact preference, and skip validation entirely otherwise. This keeps the schema in sync with form state without requiring manual imperative checks outside of Yup.
Can Yup tell me if a phone number is actually active or reachable?
No. Yup performs syntactic validation only: it checks whether a string looks like a valid phone number, not whether the number is assigned, active, or reachable. To verify that a number is real and reachable, you need a server-side API like Abstract's Phone Validation API, which queries live telecom data across 190+ countries and returns carrier, line type, and active status. The recommended pattern is to do a lightweight format check with Yup on the client, then confirm the number server-side before storing it.
How do I combine Yup validation with Abstract's Phone Validation API?
First validate the phone number's format on the client using a Yup schema with .matches() or a libphonenumber-js .test(). Once the format check passes, send the number to Abstract's Phone Validation API from your server or a serverless function so the API key is never exposed in the browser. The API returns a JSON response with fields like valid, line_type, carrier, and country. You can then surface a server-side error to the user if the number is flagged as invalid or unassigned.


