Skip to content
Home » How to Validate Phone Number Using PHP

How to Validate Phone Number Using PHP

  • by
validate phone number using PHP

In this article, we will learn about validate phone number using PHP.

It is important to make sure that your customer numbers are valid. In order for them to reach a human on the other side of their phone or computer screen.

You need an actual physical person there answering questions about what they want and how much it’s going to be before getting started with any transaction. Why wouldn’t businesses do everything possible in advance?

This includes making sure every single one has been reached by checking through several sources including public records online as well as whatever information might have come into question via callers.

Validate Phone Number Using PHP

Those aren’t quite right themselves but still may know someone else able enough at finding out things such as us neighbors/friends etc..

The debate about a programming language being good or bad is never-ending since the verdict depends upon countless factors.

One such controversial language is PHP.

PHP, which is an acronym for Hypertext Preprocessor, is an open-source scripting language that is used widely around the globe.

Furthermore, PHP is free to use and download and all scripts are executed on the server.

Many other factors make PHP a popular language.

For example, It allows users to create page dynamic page content, create forms, send/receive cookies, encrypts data, controls user access, etc.

What makes it stand out from HTML is that using PHP, you can give outputs that include PDF files, images, flash movies, etc

Other than that, it also allows you to output texts such as XHTML and XML.

There might be some beginners who are wondering why they should learn PHP.

  • To answer that question, look at the following points:
  • PHP is compatible with almost all servers
  • Supports a wide range of database
  • Easy to learn
  • Runs efficiently on the server-side
  • The latest version (PHP 7) has improved error handling, strict type declarations for function arguments, and new operators such as the spaceship operator.

Can you validate a phone number in PHP?

Sure thing, here are five quick steps for validating any four-digit string of numbers.

Nose everyone’s digits and check if they’re unique by making sure there is no multiple occurrences within this range anywhere else on file or even outside it as well (for example overseas).

If everything looks good then congratulations because that means we have ourselves one seriously reliable human being with an amazing knack at remembering just what goes into those little boxes!

Validate Phone Number Using PHP

Now that we have learned what makes PHP unique, let us talk about a common issue that our programmers face today.

The problem that I’m talking about is “how to validate a phone number using PHP?”.

There are many instances where a web developer will need to validate a phone number that he receives through a form.

This article will discuss how you can easily solve this problem so without further ado, let us get straight into it.

function validate_phone_number($phone)
{
     // Allow +, - and . in phone number
     $filtered_phone_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
     // Remove "-" from number
     $phone_to_check = str_replace("-", "", $filtered_phone_number);
     // Check the lenght of number
     // This can be customized if you want phone number from a specific country
     if (strlen($phone_to_check) < 10 || strlen($phone_to_check) > 14) {
        return false;
     } else {
       return true;
     }
}

Explanation:

A clever and efficient trick that a developer may use is to allow the user to enter his phone number using the country code and in some cases, using the “+” sign before the country code.

To do this, we will use a PHP function.

In the very first step, the function has made to take the phone number as a parameter.

Furthermore, to make this program even more efficient, by using the FILTER_SANITIZE_NUMBER_INT filter.

We have restricted to use of special characters so that the input is given in numbers, “+”, “-” and “.” signs only.

The reason why “+” and “-” signs allow is that sometimes phone numbers can be written in the form of +92-xxxx-xxxxxxx.
Now, we tell our code to remove the “-” sign from the phone number. The user has entered and to do this. We make use of a special PHP function called “str_replace”.

Next, our program checks whether the entered phone number is of the right length, and based on that, it returns a value of either True or False.

The length of a phone number is usually 10 to 14 characters long depending upon the country code.
Conclusion:
The above code will help you validate a phone number properly using PHP.

Read more: How to Convert a Python String to Integers?

 Validate phone number with country code in PHP

There are a few ways to validate a phone number with a country code in PHP. One way is to use the built-in FILTER_VALIDATE_REGEXP filter, which allows you to specify a regular expression that the input must match. For example, to validate a U.S. phone number, you could use the following regular expression:

^\+1\d{10}$

This would require the input to start with +1, followed by 10 digits (NO spaces or other characters). If the input doesn’t match this regular expression, it will fail validation.

Another way to validate a phone number is to use the Intl extension’s lib phone number library.

<?php function checkPhoneNumber($phoneNumber) {
$countryCode = substr($phoneNumber, -2);
if ($countryCode != "US")
{ return false;
}
$areaCode = substr($phoneNumber, -3);
if (!preg_match("/^([2-9]|[0-9])\d{3}$/", $areaCode))
{ return false;
}
$phoneNumber = substr($phoneNumber, 0);
return (checkPhoneNumber($areaCode) && checkPhoneNumber($phoneNumber)); }
?>
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *