Безобидный validation php. Валидировали, валидировали… и вывалидировали! Сравниваем валидаторы данных в PHP

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.

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.

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.

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”.

So how was that? We have just seen the validation of mobile number in PHP.

Did you enjoy this post? Please let me know if you want any improvement in the code to validate a phone number using PHP.

This and the next chapters show how to use PHP to validate form data.

PHP Form Validation

Think SECURITY when processing PHP forms!

These pages will show how to process PHP forms with security in mind. Proper validation of form data is important to protect your form from hackers and spammers!

The HTML form we will be working at in these chapters, contains various input fields: required and optional text fields, radio buttons, and a submit button:

The validation rules for the form above are as follows:

Field Validation Rules
Name Required. + Must only contain letters and whitespace
E-mail Required. + Must contain a valid email address (with @ and .)
Website Optional. If present, it must contain a valid URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must select one

First we will look at the plain HTML code for the form:

Text Fields

The name, email, and website fields are text input elements, and the comment field is a textarea. The HTML code looks like this:

Name:
E-mail:
Website:
Comment:

Radio Buttons

The gender fields are radio buttons and the HTML code looks like this:

Gender:
Female
Male
Other

The Form Element

The HTML code of the form looks like this: