How to Validate Phone Numbers in Laravel: A Step-by-Step Guide
Laravel

How to Validate Phone Numbers in Laravel: A Step-by-Step Guide

Phone number validation is a crucial aspect of any web application, especially when dealing with user registration and communication. In this article, we will explore how to validate phone numbers in Laravel, a popular PHP web application framework. We will create a custom phone number validation rule and demonstrate how to use it in your Laravel projects.

Why Validate Phone Numbers?

Validating phone numbers is essential to ensure that the data collected from users is accurate and can be used effectively for communication purposes. By validating phone numbers, you can

1. Prevent incorrect or fake phone numbers from being entered into your database.

2. Ensure that users receive important notifications and alerts via SMS or phone calls.

3. Enhance the user experience by providing immediate feedback on the validity of the entered phone number.

Getting Started

Before we dive into phone number validation, make sure you have Laravel installed. If you haven't already, you can install Laravel by following the official [Laravel Installation Guide](https://laravel.com/docs/8.x/installation).

Creating a Custom Validation Rule

Laravel allows you to create custom validation rules easily. To create a custom phone number validation rule, open your terminal and run the following command to generate a new rule:

php artisan make:rule PhoneNumber

This command will create a new file named `PhoneNumber.php` in the `app/Rules` directory.

Now, open the `PhoneNumber.php` file and define your custom validation rule logic in the `passes` method. Here's an example of a simple phone number validation rule using regular expressions:

use Illuminate\Contracts\Validation\Rule;



class PhoneNumber implements Rule

{

    public function passes($attribute, $value)

    {

        // Define a regular expression pattern for valid phone numbers

        $pattern = '/^\d{10}$/';



        // Use preg_match to check if the value matches the pattern

        return preg_match($pattern, $value);

    }



    public function message()

    {

        return 'The :attribute must be a valid 10-digit phone number.';

    }

}

In this example, the `passes` method uses a regular expression to check if the provided phone number consists of exactly 10 digits. You can modify the regular expression pattern to suit your specific requirements.

Using the Custom Validation Rule

Now that you've created your custom phone number validation rule, let's use it in a Laravel validation request. Assume you want to validate a phone number in a user registration form.

1. Create a new validation request using the following command:

php artisan make:request RegistrationRequest

2. Open the generated `RegistrationRequest.php` file located in the `app/Http/Requests` directory.

3. Inside the `rules` method of the `RegistrationRequest` class, add a validation rule for the `phone_number` field using your custom rule:

use App\Rules\PhoneNumber;



public function rules()

{

    return [

        // Other validation rules...

        'phone_number' => ['required', new PhoneNumber],

    ];

}

Now, when you submit the registration form, Laravel will automatically validate the phone number field using your custom validation rule.

Displaying Validation Errors

To display validation errors in your view, you can use Laravel's built-in validation error messages. Here's an example of how to display validation errors for the `phone_number` field in a Blade template:

@if ($errors->has('phone_number'))

    <div class="alert alert-danger">

        {{ $errors->first('phone_number') }}

    </div>

@endif

Conclusion

Validating phone numbers in Laravel is an essential part of building robust web applications. By creating a custom validation rule, you can ensure that the phone numbers collected from your users are accurate and ready for communication.

In this article, we've covered how to create a custom phone number validation rule in Laravel and integrate it into a registration form. By following these steps, you can enhance the data quality of your Laravel applications and provide a better user experience.

Get The latest Coding solutions.

Subscribe to the Email Newsletter