How to validate email patterns in Laravel?

Laravel provides many ways of validating user input. Email validation is one of the most important input validations. It assures that user input for emails contains the correct email pattern. We can use regex patterns or the Laravel-supported email rule to validate email in Laravel.

You can define Laravel validation rules in the Controller class. Apply the rules before saving or storing user data in the application and database. Validation rules can be applied to many types of inputs from a user.

There are separate rules for mobile numbersmaximum character validations, and image and video uploads. For example, checking image dimensions. Let’s check the rule to validate email patterns.

Email validation rule

We can pass the email validation rule for fields that contain email inputs.

$validator = Validator::make($request->all(), [
        'Email'=>'required|email'
 ]);

This rule is a Laravel build function that automatically checks if the input value has the correct email pattern. We won’t need to write a customized function to check for string literals in an email. However, we can also use a regular expression to check for email patterns.

Regex rule to validate email

We can pass the regex function and a string for email pattern match.

$this->validate($request, [            
  'email' => 'required|regex:/(.+)@(.+)\.(.+)/i',
])

The above code checks if the email field is not empty and has the input matching the pattern above. The regex pattern is to validate email inputs.