Guides
Last Updated Sep 03, 2023

How to Format Phone Numbers & Successfully Validate Them

Shyam Purkayastha

Table of Contents:

Get your free
API
key now
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
Get your free
Phone Validation API
key now
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

Phone numbers have different formats. Usually, it is the result of various regional conventions or local number dialing sequences. For an online business capturing phone numbers of leads or customers, such inconsistencies in phone numbers format have an adverse impact on their marketing campaigns.

In this post, we will show you how to leverage Abstract Phone Number Validation and Verification API to check for valid phone number formats. Apart from checking the validity of the format, this API also provides useful information related to the location and carrier of the phone numbers. You will also witness how easy it is to integrate this API within your phone number validation API endpoints to return clear and explicit error messages regarding any phone number.

Let’s send your first free
API
Phone Validation API
call
See why the best developers build on Abstract
Get your free api

What is the Correct Format for Phone Numbers?

A phone number is an address that uniquely identifies a phone subscriber anywhere in the world. It has three parts. The first one is the country code. This is the international access code identifying the country where the phone number is located. It is followed by an area code and the subscriber number.

However, there are certain exceptions while writing phone numbers that treat some parts as optional. This is due to the dialing conventions based upon the location of the caller and callee. Accordingly, the phone number formats may differ. For example, if both the caller and callee are located in the same country, then the country code part of the phone number is optional while dialing. This is the local phone number format.

Similarly, other phone numbers are reserved for special scenarios, such as emergency services. The 911 number is the best example of such a service. These numbers are not meant for phone subscribers, and they do not follow the usual addressing format based on country or area codes.

Across the world, phone number formatting is done as per the E.164 standard. This is the global ITU (International Telecommunication Union) defined standard for representing a phone number connected to a PSTN (Public Switched Telephone Network). For the benefit of human understanding, phone number formats are partitioned in a three-part format, with varying number digits. Additionally, different countries in the world have specific dialing rules and conventions that necessitate the addition of prefix digits in phone numbers.

US Phone Number Format

In the US, phone numbers have the standard three-part format. However, for representational purposes, the subscriber number is split into an exchange code and the line number. Thus, a US phone number has four parts. The country code, followed by the three digit area code, the three digit exchange code, and the four digit line number. 

The US phone number format follows the North American Numbering Plan convention adopted by the US, Canada, and the other countries of the North America and Caribbean region.  

European Phone Number Format

The EU region follows the standard three-part format. The number of digits varies from country to country. Also, different countries have slightly different conventions. For example, in the UK the mobile and pager numbers are prefixed with a 0 for the area code while dialing within the country. For international calling, this is not required.

International Phone Number Format

International phone numbers follow the standard three-part phone numbering system with varying numbers of digits based on the area and population. Phone numbers in China are 10 or 11 digits long, excluding the country code. Singapore has 8 digits. Most CommonWealth countries follow the UK convention for phone number dialing.    

The Importance of Correct Phone Number Data

Due to the varying dialing conventions, phone numbers, especially those of local leads, are often captured in their local format without the complete address containing the country code or area code. For those who offer phone services for business within a specific region, this is not a problem. However, for the sake of consistency, it is always a good practice to capture the phone number with all the parts.

Here are some reasons why phone numbers should be in the correct format for business reasons.

Better Readability

Phone numbers defined with the standard format represent a globally unique phone subscriber. 

Omitting the country code or area code during phone number capturing leads to ambiguity. For a business that relies on making a connection with their customers via a telephone number, this poses a great problem in deciphering the actual dialing pattern in the list of phone numbers, if all numbers do not follow the globally unique number format. 

Targeted Messaging

Phone numbers contain additional information about the location and carrier of the subscriber. Therefore, by capturing the number in complete format, it is possible to devise targeted messaging for the subscriber. This approach works great for marketing campaigns that rely on customers’ phone numbers. A local phone number may not be able to provide this additional information.

Maintaining Sanity of Contact lists

Phone numbers captured through standard web forms can only check for the number format. There is no way to check the validity of the area codes, or line numbers just by looking at the phone number. Overlooking the additional checks can lead to junk phone numbers swelling up the contact lists, eventually impacting the conversion rates for marketing campaigns. 

That’s where Abstract Phone Number Validation and Verification API comes to the rescue. The API takes care of the heavy lifting checks for ascertaining the legitimacy of a phone number to ensure that the contact lists are devoid of fake and invalid numbers. 

How to Validate Phone Numbers

Building a validation logic for phone numbers is quite easy. Assuming there is a form to capture the phone number of a visitor on a website, it is possible to hook it to a custom API that validates the phone number before saving it in the database.

Here is a quick Node.js/Express.js code that defines an API endpoint ‘POST / validate-phone’. It accepts the phone number as a request parameter and returns a 200 OK success or 400 failure response depending upon the phone number’s validation result. Behind the scenes, it leverages the Abstract Phone Number Validation and Verification API to make the validation decision.


const express = require('express')
const bodyParser = require('body-parser');
const axios = require('axios')
const app = express()

const port = 3000

const ABSTRACT_API_KEY = "<YOUR_ABSTRACT_API_KEY>"

app.use(bodyParser.json());



app.post('/validate-phone', async (req, res) => {

          
          axios.get('https://phonevalidation.abstractapi.com/v1/?api_key=' + ABSTRACT_API_KEY + '&phone=' + req.body.phone)
          .then(response => {

              console.log(response.data);

              if(response.data.valid){
                  console.log("Added New Subscriber " + req.body.phone + " to Database")  
                  return res.status(200).json({ status: "success", errors: " " });  
              } else {
                  console.log("Invalid Phone Number Detected")
                  return res.status(400).json({ status: "fail", error: "invalid phone number"}); 
              }
              
          })
          .catch(error => {
              console.log(error);
              return res.status(500).json({ status: "fail", error: "internal server error" }); 
          });
          
          

})



app.listen(port, () => {
  console.log(`Phone Validation Server Listening on Port ${port}`)
})

To run this code, initialize an empty Node.js project and save the above file as ‘app.js’ after replacing the placeholder “YOUR_ABSTRACT_API_KEY” with the actual API key. You can get your exclusive API key after signing up and accessing the API tester page

After that, install the following dependencies:


npm install express axios

This will install the express and axios libraries needed to host the API endpoint and make the call to Abstract API respectively.

Run the application with ‘node app.js’ command. It will run as a localhost server on port 3000. Now you can trigger the ‘/validate-phone’ API endpoint by passing the request body with a JSON envelope containing the key “phone” and value as the actual phone number to be validated. For a valid phone number, you will get a successful response.

The server logs will display the actual data returned by the Abstract Phone Validation and Verification API containing additional information about the phone number in JSON format.

The value of the field “valid” indicates the validity of the phone number. The above code example relies on this value to decide whether the phone number is valid or not. Accordingly, a success or failure message is returned in the API response.

FAQs

What Does Validate Your Phone Number Mean?

Validating a phone number entails checking for the format and existence of the phone number. Phone numbers are formatted in a consistent manner in line with international numbering standards defined in E.164 ITU specifications. But even if the phone number format is correct, identifying the existence of the phone address line is important to ensure that the phone number is allocated to a subscriber. With the help of Abstract Phone Validation and Verification API, you can easily validate both the format and existence of any phone number in the world.

What is the Purpose of Validation?

10 digit Phone number validation is important for certain use cases related to online marketing and list building. Marketers building phone number lists frequently face the problem of fake phone numbers. Since it is not practical to call and validate every phone number manually, there is a need for a better option. With Abstract Phone Number Validation and Verification API, it is possible to automate the phone number validation tasks and get instant feedback on any given phone number without calling the number.

How Can I Check the Validity of a Mobile Number?

You can easily check the validity of a mobile number with the Abstract Phone number Validation and Verification API. Apart from detecting the legitimacy of a mobile number, this API also provides additional information about a number, such as location and mobile operator details.

4.5/5 stars (9 votes)

Shyam Purkayastha
Shyam Purkayastha is a proficient web developer and PHP maestro, renowned for his expertise in API creation and integration. His deep knowledge of PHP scripting and backend development enables him to build scalable server-side applications. Shyam is particularly passionate about RESTful API design, significantly enhancing web service functionality and data interoperability.
Get your free
Phone Validation API
API
key now
Abstract’s Phone Number Validation and Verification API will help you improve your contact rate and clean lists by ensuring that only verified numbers are included in the database.
get started for free

Related Articles

Get your free
API
Phone Validation API
key now
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