Created: February-27, 2022 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. 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.Use regex Regular
Expression to Validate Phone Numbers in PHP
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 formatThe 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 formatIt 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-3333333The 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.