Preg_match phone number validation php

  1. HowTo
  2. PHP Howtos
  3. Validate Phone Number in PHP

Created: February-27, 2022

  1. Use regex Regular Expression to Validate Phone Numbers in PHP
  2. Use the filter Method to Validate Phone Numbers in PHP

PHP has two ways to validate phone numbers, one is the regular expression regex, and the other is the filter method. We can set a template and validate phone numbers according to that template with regex, but filter will only exclude unwanted characters.

This tutorial demonstrates how to validate different phone numbers in PHP.

Use regex Regular Expression to Validate Phone Numbers in PHP

The preg_match() is a built-in function in PHP that checks if the data is according to the given format; it returns a Boolean value.

Example:

<?php
function validate_number($phone_number){
if(preg_match('/^[0-9]{11}+$/', $phone_number)) {
    // the format /^[0-9]{11}+$/ will check for phone number with 11 digits and only numbers
    echo "Phone Number is Valid <br>";
}   else{
    echo "Enter Phone Number with correct format <br>";
    }
}
//valid phone number with 11 digits
validate_number("");
//Invalid phone number with 13 digits
validate_number("33");
// 11 digits number with invalid charachters.
validate_number("03333333-33");
?>

The code above will only validate a phone number with 11 digits only.

Output:

Phone Number is Valid
Enter Phone Number with correct format
Enter Phone Number with correct format

The next example shows how to validate a phone number that begins with a - and includes special characters and country codes.

Example:

<?php
function validate_number($phone_number){
if(preg_match('/^[0-9]{4}-[0-9]{7}$/', $phone_number)){
    // the format /^[0-9]{4}-[0-9]{7}$/ will check for phone number with 11 digits with a - after first 4 digits.
    echo "Phone Number is Valid <br>";
}   else{
    echo "Enter Phone Number with correct format <br>";
   }
}
//Valid phone number with 11 digits and a -
validate_number("0333-3333333");
//Invalid phone number with 13 digits and -
validate_number("0333-333333333");
//Invaild  11 digits number with two -.
validate_number("0333-3333-33");
echo "<br>";

function validate_country_number($phone_number){
if(preg_match('/^\+[0-9]{1,2}-[0-9]{3}-[0-9]{7}$/', $phone_number)){
    // the format /^\+[0-9]{1,2}-[0-9]{3}-[0-9]{7}$/ will check for phone number with country codes, 11 or 12 digits, + and -
    echo "Phone Number is Valid with country code <br>";
}   else{
    echo "Enter Phone Number with correct format <br>";
   }
}
//Valid phone number with 12 digits + and -. According to the format given in the function for country code.
validate_country_number("+92-333-3333333");
//Invalid phone number with with country code
validate_country_number("+92-333333333333");
//Invaild  Number without country code.
validate_country_number("");
?>

Output:

Phone Number is Valid
Enter Phone Number with correct format
Enter Phone Number with correct format

Phone Number is Valid with country code
Enter Phone Number with correct format
Enter Phone Number with correct format

It should be mentioned here every country has its format of phone numbers. Any format can be set preg_match regex function to validate a phone number.

Use the filter Method to Validate Phone Numbers in PHP

Filters in PHP are used to validate and sanitize the data inputs. Filters also correct the format and output the correct phone number.

Example:

<?php
function validate_number($phone_number){
$valid_phone_number = filter_var($phone_number, FILTER_SANITIZE_NUMBER_INT);
echo $valid_phone_number."<br>";
}
//valid phone numbers
validate_number("");
validate_number("0333-3333333");
validate_number("+92-333-3333333");
//invalid phone numbers
validate_number("03333333&333");
validate_number("0333-33*33333");
validate_number("+92-333-333##3333");
?>

The code above uses the built-in function filter_var with the constant FILTER_SANITIZE_NUMBER_INT, which will check for integers with + and - signs. If it detects any other characters, it will exclude them and return the correct phone number.

Output:


0333-3333333
+92-333-3333333

0333-3333333
+92-333-3333333

The back draws of this method are we cannot set a length for a phone number, and if the + and - are at the wrong places in the number, it will not correct them. The filter method can be used when we mistakenly enter other characters, excluding them.

Preg_match phone number validation php

How to validate a mobile number in PHP?

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.

How can I set 10 digit mobile number in PHP?

php $value = '9987199871'; $mobileregex = "/^[1-9][0-9]{10}$/" ; echo "Hi " .

How do I validate a mobile number?

Mobile Number validation criteria: The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.

What is the use of Preg_match in PHP?

The preg_match() function returns whether a match was found in a string.