Validate Email address in PHP with multiple methods

While taking an E-mail address as user input, it’s essential to validate it properly otherwise it can affect application functionality. In this article, we will validate the Email address in PHP with regular expressions and also with the FILTER_VALIDATE_EMAIL function. You can implement those methods in other PHP-based frameworks like Laravel or CI.

Here, we will see two methods. Both check URL is valid or not using PHP functionality.

  • Validate Email Using Filter (FILTER_VALIDATE_EMAIL)
  • Validate Email Using Regex

The Filter method is built in PHP. Which provides various filter validation like email, URL, and more. On the other hand, the preg_match() or regular expression is aimed to search patterns into strings and return a flag based on results. It will return True if a match is found otherwise, false.

To be clear, those methods will validate the format of the email address rather than a valid email.

Validate Email Using Filter (FILTER_VALIDATE_EMAIL)

We can validate email addresses using the filter_var() method and FILTER_VALIDATE_EMAIL filter. Here, the function will take the email address as an argument and validate the email against the syntax in RFC 822. We can test the validation of email using a valid and an invalid email.

In this tutorial, we will define email addresses statically rather than form input. You can use it as per your requirement. Let’s take an example to validate email address using this method:

<?php
    function validateEmail($email) {
        if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo "Valid Email<br>";
        }
        else {
            echo "Invalid Email<br>";
        }
    }
    validateEmail('alex@google.com');
    validateEmail('roxxie@google.123');
?>

Valid Email
Invalid Email

In the above example, we have created a function that implements our logic to validate email. Here, we just print messages based on validation. As you can see while calling this function first time it will show a valid address. But when we call a function using a second email then it will show the email address as invalid because it doesn’t match RFC 822 syntax.

Validate Email Using Regex

Same as java-script or any other programming language, we can also use the preg_match() function to validate an email address. It will use the regular expression for the validation rule of the email. We can use this method and define custom rules for validation.

It will take two parameters, The First one is a regular expression and the second one is our email address which we want to validate. Let’s create the same function as the above example and validate email addresses.

<?php
    function validateEmail($email) {
        $regex = "/^([a-zA-Z0-9\.]+@+[a-zA-Z]+(\.)+[a-zA-Z]{2,3})$/";
        if(preg_match($regex, $email)){
            echo "Valid Email<br>";
        }
        else {
            echo "Invalid Email<br>";
        }
    }
    validateEmail('alex@google.com');
    validateEmail('roxxie@google.123');
?>

It will print the same output as above but here it will validate using regular expression. It will return invalid for the second address because we added a rule that the domain name can not be numeric.

Conclusion

In this article, we have validated Email addresses using the filter function and regular expression. The filter method is idle for this purpose because of the flag and it’s easy to implement.