Laravel 12: Validate Email, Phone, and Password Like a Pro

As a Laravel developer, ensuring that user inputs are valid is crucial for building secure and reliable applications. In this article, I'll walk you through the process of validating email addresses, phone numbers, and passwords in Laravel 12.

We'll explore both basic and advanced validation techniques, including the use of Form Request classes for cleaner code.

Laravel 12: Validate Email, Phone, and Password Like a Pro

Laravel 12: Validate Email, Phone, and Password

Basic Validation Using validate() Method

Laravel provides a straightforward way to validate incoming request data using the validate() method. Here's how you can implement basic validation for email, phone, and password fields.

public function store(Request $request)
{
    $validatedData = $request->validate([
        'email' => 'required|email|unique:users,email',
        'phone' => 'required|digits:10',
        'password' => 'required|min:8|confirmed',
    ]);

}

Explanation:

  • required: Ensures the field is not empty.

  • email: Validates that the input is a valid email format.

  • unique:users,email: Checks that the email is unique in the users table.

  • digits:10: Ensures the phone number is exactly 10 digits.

  • min:8: Password must be at least 8 characters long.

  • confirmed: Requires a matching password_confirmation field

 

Advanced Validation Using Form Request Classes

For more complex applications, it's advisable to use Form Request classes to handle validation logic. This approach keeps your controller methods clean and promotes reusability.

Step 1: Create a Form Request Class

php artisan make:request StoreUserRequest

 

Step 2: Define Validation Rules

In app/Http/Requests/StoreUserRequest.php

public function rules()
{
    return [
        'email' => 'required|email|unique:users,email',
        'phone' => 'required|digits:10',
        'password' => 'required|min:8|confirmed',
    ];
}

 

Step 3: Use the Form Request in Controller

public function store(StoreUserRequest $request)
{
    // Validation is automatically handled
    // Proceed with storing the user
}

Benefits:

  • Keeps controller methods concise.

  • Centralizes validation logic.

  • Facilitates reuse of validation rules.​

 

Custom Validation Rules

Sometimes, built-in validation rules may not suffice. Laravel allows you to create custom validation rules to handle specific scenarios.​

Example: Prohibit Specific Usernames

Step 1: Create a Custom Rule

php artisan make:rule ProhibitedUsername

 

Step 2: Define the Rule Logic

In app/Rules/ProhibitedUsername.php

public function passes($attribute, $value)
{
    return !in_array(strtolower($value), ['admin', 'superuser']);
}

public function message()
{
    return 'The :attribute is prohibited.';
}

 

Step 3: Apply the Custom Rule

use App\Rules\ProhibitedUsername;

$request->validate([
    'username' => ['required', new ProhibitedUsername],
]);

 

Phone Number Validation

Validating phone numbers can be more complex due to varying formats. For Indian phone numbers, you can use a regular expression.

'phone' => ['required', 'regex:/^[6-9]\d{9}$/'],

Explanation:

  • regex:/^[6-9]\d{9}$/: Ensures the number starts with 6-9 and is followed by 9 digits, totaling 10 digits.

 

Password Confirmation

To ensure users correctly confirm their passwords during registration, use the confirmed rule.

'password' => 'required|min:8|confirmed',

 

Conclusion

Validating user input is a fundamental aspect of building secure Laravel applications. By leveraging Laravel 12's validation features, including Form Request classes and custom rules, you can ensure data integrity and enhance user experience.

 

Frequently Asked Questions

  • How do I validate an email in Laravel 12?
    Use required|email|unique:users,email in your validation rules.
  • How to validate phone numbers in Laravel 12?
    Use a regex like regex:/^[6-9]\d{9}$/ for Indian formats.
  • What is the best way to validate passwords in Laravel 12?
    Use required|min:8|confirmed for strong validation.

 

For more detailed information, refer to the official Laravel documentation on validation.


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS