PHP email validation the easy way



PHP email validation script

Here’s a nice little PHP email validation function to validate email addresses in PHP quickly. There is a lot of articles all around the internet about regular expressions and other tricky email validation functions. Maybe for some reason you might need it for your project, but the solution in this article gives you a basic and stable validation of email addresses with PHP. Simply copy the functions to your own project classes and go. The PHP function uses the Validate Filters of PHP. In combination with PHP 7, this is also one of the fastest solutions nowadays.

PHP email validation function

Here is the code snippet to use in your own PHP project.


function validateEmail($email){
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    else return true;
}

// Usage example
if(validateEmail("info@ihatespam.com")){
    echo "Yes, it's a real email address";
}
else{
    echo "Ooops, please try again!";
}

What are the typical uses for PHP email validation?

Of course you could also validate email addresses with JavaScript, directly in the browser of the viewer. Although this technique checks if the user has entered a correct email address, it can not check the content sent to our web server. If the validation of the email address takes place only in the browser of the visitor, this can introduce theoretically dangerous code into our system. Such insecure forms make it very easy for hackers to influence or even completely destroy a website.

Therefore, the server-side check, e.g. with PHP, the most meaningful solution. With an email address, people can identify worldwide. Whether online shopping, banking, exploring the social network or just while surfing. Every day, millions of people enter their email address into a form or login area.

Write a comment!

Comment