pakainfo
Posted on November 24, 2021
Mobile number validation pattern in PHP
You can preg_match() to validate 10-digit mobile numbers:
preg_match('/^[0-9]{10}+$/', $mobile)
Here in this post, we are going to see how to validate phone number in PHP. In many situations, web developers need to validate the phone number that is submitted through a form.
So in these cases, we must have to confirm that the phone number submitted is in the valid structure or pattern.
Phone number validation in PHP is not a hard task. We are going to do it in a simple and easy way.
We all know that a phone number is generally a 10 digits number. But there are lots of exceptional may occur and for this reason, it is not enough just to check if the number is an integer of 10 in length.
php validate phone number
Sometimes a user may submit the number with country code or sometimes the “+” sign before the country code. So it may look like the more complicated task to validate.
But, don’t worry. Here you will get the ready to use PHP function which contains only a few lines of code.
Email validation in PHP using FILTER_VALIDATE_EMAIL
Here in this post, we are going to show you the example code which will do our task.
We are going to create a function which can be used to validate a mobile number. Below is the given code of the function:
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;
}
}
The above function will take the phone number as the parameter. Inside the function, we have removed all the illegal characters from number so that it only accepts “+”, “-” and “.” using FILTER_SANITIZE_NUMBER_INT filter. Well, you may notice, the phone number may be written like this format – +91-523-452-5555 in many cases. That’s why we have allowed “+” and “-” in the phone number.
Then we have removed “-” from the phone number by using str_replace PHP function.
Generate PDF from HTML template in PHP using Dompdf
How to send email using PHP mail() function?
After that, we have checked the length and return true or false depending upon the length. You may notice that the length should be between 10 to 14. This is because generally, a number is 10 characters and with the country code it may be up to 14.
Now below is the usage of the function that we have just created:
$phone = "+91-444-444-5555";
if (validate_phone_number($phone) == true) {
echo "Phone number is valid";
} else {
echo "Invalid phone number";
}
The above code will return “Phone number is valid” as it returns true. If it would be false then it would return “Invalid phone number”.
Ref Example : php validate phone number
Posted on November 24, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.