1. Create the function
Create a PHP validate phone number function, called validating(), with a parameter of $phone.
2. Create the statement
Then you have to create an if/else statement for determining whether the given phone number is valid or not. Let’s say that the genuine phone number is 10 digits long, ranging from 0 to 9. To perform the validation, you have to utilize this regular expression: /^[0-9]{10}+$/. Also, you have to use the reg_match() function.
If the given telephone number matches the specified regular expression, it will print “Valid Phone Number.” Otherwise, it will print “Invalid Phone Number” as the error message.
3. Call the function
Now, let’s try verifying different phone numbers by calling our validating() function for number validation in PHP:
4. You will see this output:
Overall, the following code will look like this:
That’s it! You have learned the way of validating phone numbers in PHP using regular expressions. The same rules apply to mobile number validation.
Format variation of phone numbers
Phone numbers have different formats. So, verifying them could have been really challenging. Fortunately, using a PHP function has made the process very simple.
As said earlier, the in-built PHP filter can make it easier for you to validate phone number. Here, you will use it to validate numbers with different formats.
Cell phones
1. First, you have to create a function, called validating().
2. Create a variable, called valid_number. It will have the filter_var() function with a FILTER_SANITIZE_NUMBER_INT constant.
3. Now, you can print the valid_number variable.
4. Finally, let’s call the validating() PHP function with for mobile number validation.
You will see this output: 101-666-0260
Overall, the code will look like this:
That’s how you validate a cell phone number with the in-built PHP filter.
Special characters
A phone number can have certain characters, like + and -. But what will happen if the user enters the number incorrectly with special characters, like $ and *? You need to find a way to filter them out.
By using filter_var() function, you can strip them off from the given phone number.
Let’s try validate a phone number that contains $ and *, like this: 101$777*0280
Let’s use this code:
You will see an output with $ and * being removed from the given phone number: 1017770280
International numbers
International phone numbers start with + with some also needing the area code. So, you need to figure out a way to prevent it from being filtered out. Fortunately, filter_var() function supports the + sign. So, you will never have to worry about it being stripped off.
Let’s try performing something for international users, for example an Indian phone number validation. The number looks like this: +91-202-5555-0234
As you can see, the country code of India is +91. How will filter_var() function react if an Indian phone number is entered? Will it filter out the + sign?
Let’s use this code to figure out what happens:
You will see an output like this: +91-202-5555-0234
As you can see, the filter_var() function supports the + sign. So, it’s not being filtered out.
Alternative Solution
Validate phone number with an API
By using APIs, you can validate phone numbers effortlessly. They come with all the necessary functions for verification. You just need to enter the number. The phone number verification API will handle the rest. You don’t have to write any code from scratch. So, you can validate the phone numbers effortlessly.
You can try using Abstract’s phone number validation and verification API. It is reliable, blazing-fast, and easy to maintain. It can help you to validate phone numbers effortlessly.
Let’s use Abstract’s Phone Number Validation and Verification API to validate this phone number: 14154582468.
If the phone number request is successful, you will see this output:
The given phone number is valid, as the "valid" field is set to “true.” Also, it provides you with a variety of geolocation data, including country name and registered location of the phone number. You can utilize this information to enhance your marketing strategy.
As you can see, you don’t have to write any code from scratch. There is no need to utilize regular expressions or PHP in-built functions. You just need to get the API key and enter the phone number that you want to verify. Abstract’s Phone Number Validation and Verification API will handle the rest.