Validating phone numbers in PHP
PHP has two ways to validate phone numbers, one is regular expression regex
and the other is filter
the method. We can use to regex
set a template and validate the phone number based on that template, but filter
will only exclude the unwanted characters.
This article explains how to validate different phone numbers in PHP.
regex
Validating phone numbers using regular expressions
in PHP
preg_match()
Is a built-in function in PHP that checks whether the data conforms to a 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("03333333333");
//Invalid phone number with 13 digits
validate_number("0333333333333");
// 11 digits number with invalid charachters.
validate_number("03333333-33");
?>
The above code will only validate a phone number with only 11 digits.
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 starts with and contains special characters and a country code.
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("03333333333");
?>
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 that each country has its own format for phone numbers. Any format can be set with preg_match
the regular expression function to validate phone numbers.
filter
Validate phone number
in PHP using method
Filters in PHP are used to validate and sanitize data input. The filter also corrects the format and outputs 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("03333333333");
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 above code uses the built-in function filter_var
and constants FILTER_SANITIZE_NUMBER_INT
, and it will check for an integer with the +
and -
symbols. If it detects any other characters, it will exclude them and return the correct phone number.
Output:
03333333333
0333-3333333
+92-333-3333333
03333333333
0333-3333333
+92-333-3333333
The disadvantage of this method is that we cannot set the length for the phone number, and if +
and -
are in the wrong position in the number, it will not correct them. When we enter other characters by mistake, we can use the filtering method to exclude them.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of