Home / PHP / How To Validate Phone Numbers Using PHP

How To Validate Phone Numbers Using PHP

Phone Number Validation in PHP

PHP is a very popular server-side programming language. One of the obvious task for a server-side programming language is to process forms. Forms are used to get data from website users. You could receive different inputs from what you expected due to mistakes of users. Also, hackers could use forms to access to your internal data. Hence, it is very important to validate user input data before using them for various purposes.

Phone numbers are an essential input field in many forms. There are two ways that you can use to validate the phone numbers in PHP. you can either use PHP inbuilt filters or regular expressions for that purpose.

Phone numbers are different from country to country. When you are creating a web application that will be used by people around the globe, you have to write codes which verify each of these requirements.

You might like this :

Phone Number Validation in PHP

Using inbuilt PHP filters easier. But, as there are different types of formats for phone numbers, you have to use regular expressions to validate these phone numbers. PHP provides you with several very powerful functions for parsing regular expressions.

Anyway, we will discuss both methods in this tutorial. First, we will study PHP filters.

Validating Phone Numbers with Filters

We don’t know what will user enter in the input fields. There are some certain characters that we can expect in a phone number.

Those are digits, ‘-’, ‘.’ and ‘+’. User sometimes could enter any other characters by mistake or with malicious intention.

We must remove those characters before doing any processing. PHP has built-in functions for this purpose. We call it sanitizing.

It will strip off any invalid characters in phone numbers.

This is how you going to do it.

function validating($phone){

$valid_number = filter_var($phone,FILTER_SANITIZE_NUMBER_INT);

echo $valid_number."<br>";

}

You can see that we have used the filter_var function with a  FILTER_SANITIZE_NUMBER_INT  constant.

Let’s try this out with few phone numbers.

function validating($phone){

$valid_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

echo $valid_number."<br>";

}

validating("202$555*0170");

Output

2025550170

Great!.

It strips off the `$` sign and `*` sign and returns only digits. Sometimes you may want to allow `-` in phone numbers. For example, 202-555-0170 is a valid phone number. Let’s see how `filter_var` function reacting to it.

function validating($phone){

$valid_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

echo $valid_number."<br>";

}

validating("202-555-0170");

Output

202-555-0170

Great again! We get exactly what we want. `filter_var` with `FILTER_SANITIZE_NUMBER_INT` There is one more thing. In international formatting, you need to allow + sign with country code. We have to see does filter_var supports `+` sign.

Let’s take +1-202-555-0170 and see what output does filter_var gives to it.

function validating($phone){

$valid_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

echo $valid_number."<br>";

}

validating("+1-202-555-0170");

Output

+1-202-555-0170

Excellent.

There is still a small issue. We did not check the lengths of phone numbers yet.

Let’s check the number `2025550170000`.

function validating($phone){

$valid_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

echo $valid_number."<br>";

}

validating("2025550170000");

Output

2025550170000

Well, filter_var is not able to validate the length of a phone number. You may want to validate it manually. You can see that the length of a phone number which includes country code could be between 10 and 14. Of course, this length is only valid if we remove ‘-’ in numbers. Let’s do it.

function validating($phone){

$valid_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);

$valid_number = str_replace("-", "", $valid_number);

if (strlen($valid_number) < 10 || strlen($valid_number) > 14) {

echo "Invalid Number <br>";

} else {

echo "Valid Number <br>";

}

}

validating("+1-202-555-0170000");

Output

Invalid Number

That is our final code. It gives us the expected results. Next, we will see how do we validate phone numbers using Regular Expressions

Validating Phone Numbers with Regular Expressions

A lot of beginner level developers find regular expression is difficult. Well, actually learning regular expressions is easy. Using it requires good reasoning and logic. It gives great flexibility and power to developers. Therefore, Regular Expressions are such an important tool for any developer.

In the simplest form, the phone number is a 10 digits code without any other characters.

You use the following pattern to represent that. You can use `’/^[0-9]{10}+$/’` in Regular Expressions to represent that.

PHP provides the `preg_match` function to parse Regular Expressions.

Check the following code.

function validating($phone){

if(preg_match('/^[0-9]{10}+$/', $phone)) {

echo "Valid Email <br>";

}else{

echo "Invalid Email <br>";

}

}

Let’s try this out now.

We will take several valid and invalid phone numbers and see whether our new code provides us with accurate results.

function validating($phone){

if(preg_match('/^[0-9]{10}+$/', $phone)) {

echo "Valid Email <br>";

}else{

echo "Invalid Email <br>";

}

}

validating("2025550170"); //10 digits valid phone number

validating("202555017000"); //12 digits invalid phone number

validating("202$555*01"); //10 letters phone number with invalid characters

validating("202$555*0170"); //10 digits phone numbers with invalid characters

Output

Valid Email

Invalid Email

Invalid Email

Invalid Email

Great! We get results as we want it. You can see we have covered various possible inputs in the code.

Next, we will try to validate phone numbers with 202-555-0170 format to validate.

We will have to slightly change our regular expression for that.

function validating($phone){

if(preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $phone)) {

echo "Valid Email <br>";

}else{

echo "Invalid Email <br>";

}

}

validating("202-555-0170");

Output

Valid Email

Notice that we have our changed our regular expression to `’/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/` which will strictly look for the `000-000-0000` pattern.

Finally, let’s validate a phone number which has an international code.

Some countries have international code with one number while the other countries include two numbers in their country codes.

So you should be able to take that into account when writing your regular expression.

function validating($phone){

if(preg_match('/^\+[0-9]{1,2}-[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $phone)) {

echo "Valid Email <br>";

}else{

echo "Invalid Email <br>";

}

}

validating("+1-202-555-0170");

validating("+91-202-555-0170");

Output

Valid Email

Valid Email

Conclusion

You can see how powerful Regular expressions are from these examples. You can validate any phone number against any format using Regular Expressions. On top of that PHP provides a very easy way to work with them. That’s it for Phone number validation with PHP. I will meet you with another tutorial.

Get New Freebies To Your Inbox

Get New Freebies To Your Inbox

Join our mailing list to receive the latest FREEBIES and updates from our team.

You have Successfully Subscribed!

Leave a Reply

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

Pin It on Pinterest

Shares
Share This