Multiple ways to Validate Email Addresses in Laravel 10

Websolutionstuff | Mar-04-2024 | Categories : Laravel PHP jQuery

Hey there! Have you ever wondered how websites ensure that the email addresses you provide are valid? In this article, I'll walk you through multiple ways to validate email addresses in Laravel 10.

We'll explore different techniques, from basic regex patterns to leveraging Laravel's built-in validation features.

So, let's see multiple ways to validate email addresses in laravel 10, how to validate email in laravel 8/9/10, how to validate email in PHP, email validation using jQuery, email validation regex, email validation using filter_var, and laravel email validation.

1. Regular Expression Validation

Regular expressions (regex) provide a flexible and powerful way to validate email addresses.

$email = '[email protected]';
if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) {
    echo 'Valid email address';
} else {
    echo 'Invalid email address';
}

 

2. PHP's filter_var Function

PHP provides a built-in function filter_var for basic email validation using the FILTER_VALIDATE_EMAIL flag:

$email = '[email protected]';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo 'Valid email address';
} else {
    echo 'Invalid email address';
}

 

3. Laravel Validation

In Laravel, you can utilize the validation features provided by the framework. Laravel offers a convenient way to validate user input through its Validator class:

$request->validate([
    'email' => 'required|email',
]);

This validation rule utilizes the egulias/email-validator package for validating the email address. 

'email' => 'email:rfc,dns'

 

4. DNS Lookup

Another approach is to perform a DNS lookup on the domain part of the email address to ensure it points to a valid domain. While this method can provide additional validation, it adds overhead and might not be suitable for every use case.

<?php

$email = '[email protected]';
list($user, $domain) = explode('@', $email);

// Perform DNS lookup for MX records
if (checkdnsrr($domain, 'MX')) {
    echo "Domain exists and has mail exchange (MX) records.";
} else {
    echo "Domain does not exist or does not have mail exchange (MX) records.";
}

Alternatively, you can use dns_get_record() the function to get more detailed information about the DNS records of a domain:

<?php

$email = '[email protected]';
list($user, $domain) = explode('@', $email);

// Get DNS records for the domain
$dns_records = dns_get_record($domain, DNS_ANY);

if (!empty($dns_records)) {
    echo "Domain exists and has DNS records.";
    print_r($dns_records);
} else {
    echo "Domain does not exist or does not have DNS records.";
}

 

5. Email Validation using jQuery

You can use jQuery to perform email validation on the client side before submitting a form to the server.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Validation</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
    $('#email').on('input', function() {
        var email = $(this).val();
        if(validateEmail(email)) {
            $('#email-error').text('');
        } else {
            $('#email-error').text('Invalid email address');
        }
    });

    function validateEmail(email) {
        // Regular expression for basic email validation
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
});
</script>
</head>
<body>
    <form id="email-form">
        <label for="email">Email:</label>
        <input type="text" id="email" name="email">
        <span id="email-error" style="color: red;"></span>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

 


You might also like:

Recommended Post
Featured Post
How To Get Last Record In Laravel 8
How To Get Last Record In Lara...

In this example, we will see how to get the last record in laravel 8. You can simply get the last record using laravel 8...

Read More

Jan-26-2022

How To Image Optimization In Laravel 9 Using Spatie
How To Image Optimization In L...

In this article, we will see how to image optimization in laravel 9 using spatie. Here, we will learn about image o...

Read More

Feb-10-2023

How To Add Digital Signature In PDF In Laravel 9
How To Add Digital Signature I...

In this article, we will see how to add a digital signature in pdf in laravel 9. Here, we will learn to add a digit...

Read More

Dec-21-2022

How To Install React In Laravel 9
How To Install React In Larave...

In this article, we will see how to install React in laravel 9. We will also install react with laravel 9 and...

Read More

Aug-15-2022