How to validate Phone Number and Address with PHP

November 18, 2019 12:23 pm

When you consider that over 43 percent of cyber-attacks target small businesses, you can quickly see how important proper cyber-security is. One of the best ways to ensure users on your website is legitimate and not hackers with malicious intent is by validating their phone number and address.

Validate Phone Number and Address with PHP

By making sure a new user is who they claim to be can help you avoid letting cyber-criminals onto your website or application. Most business owners use a two-factor authentication process when registering new users. Often times, one of the things this authentication process will involve is the sending of a pin code to a new user’s phone.

Are you curious about how to validate phone numbers and addresses with PHP? If so, consider the helpful tips below.

Why Validating Customer Data is Important

One of the main goals you should have with a business website is attracting new customers. Having a website that is both functional and secure is something you should view as a priority. 

Verifying and sanitizing the data consumers put into your forms is a must. By verifying the data, you can ensure it is not common from a source with malicious intent.

First Name:
Last Name:
Email Address:
Mailing Address:
Telephone Number:
Comments :

Neglecting to name each field will provide hackers with the opportunity to add more information which can be used to infiltrate your website. Denying users the ability to add any special characters or change formats is essential.

Continuously monitoring your logs will provide you with information about any problems that may exist. If there are vulnerabilities in your code, you need to find them and fix them immediately.

Validating Phone Numbers with the Right PHP Code

The first thing you have to do when attempting to validate phone numbers is to create the right type of PHP function. When creating a function to validate a phone number, it should look something like this:

function validatePhone($string) {
    $numbersOnly = ereg_replace("[^0-9]", "", $string);
    $numberOfDigits = strlen($numbersOnly);
    if ($numberOfDigits == 7 or $numberOfDigits == 10) {
        echo $numbersOnly;
    } else {
        echo 'Invalid Phone Number';
    }
}

By putting in functions that only allow for numbers to be entered into the form, you can avoid issues with text getting put into the wrong places. The last string of code is a function that will produce an “Invalid Phone Number” error message if mistakes are made by the consumer. 

With this type of code, you will be able to receive the phone number as a parameter. When writing your function, be sure to remove all illegal characters that are not commonly found in phone numbers around the world. This function also contains code that checks the length of the phone number given by a new user.

Most phone numbers are between 10 to 14 characters. If the number give is far below or above these numbers, then providing the user with an error message will give them a chance to change it.

Creating a Usage Function is Important

Putting this code into action will require you to create a usage for the function that was previously created.

$phone = "+91-444-444-5555";
 
if (validate_phone_number($phone) == true) {
   echo "Phone number is valid";
} else {
  echo "Invalid phone number";
}

The main idea behind this piece of code is to ensure the phone number is valid. If the phone number given is not valid, this code will produce an error message. Failing to validate this important information will leave your site or app at risk, which is why it is so important.

The first line of this code provides a template for what a customer’s input should look like. If the number is not entered as it is in the template, it will produce an “Invalid Phone Number” message.

Validate Phone Number and Address with PHP

Tips on Validating Addresses With PHP

Another important piece of information you need to validate is a new user’s physical address. In order to do this, you will need to use a piece of code like:

<?php
/* Validates if $zipCode is a 5 digit number in the 12345 format. Note that this simply checks to see if $zipCode is a 5 digit number, not necessarily a valid U.S. Zip Code.
*/

function validateZipCode($zipCode) {
	if (preg_match('#[0-9]{5}#', $zipCode))
		return true;
	else
		return false;
}
?>

As you can see, this code will use the zip code database to ensure the address provided is accurate. You may need to alter the code a bit to accommodate the +4 zip code inputs.

Your Hard Work Will Pay Off

While coming up with the right system to validate phone numbers and addresses can be time-consuming and difficult, it is definitely worth the effort invested. If you are unsure about how to utilizing the power of PHP on your own, consulting with a programmer with experience may be helpful. They will be able to develop code that is both effective and error-free.