Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] PHP Validate Email using filter_validate_email and regex

#1
PHP Validate Email using filter_validate_email and regex

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/08/php-validate-email-using-filter_validate_email-and-regex.jpg" width="550" height="432" title="" alt="" /></div><div><div class="modified-on" readability="7.1111111111111"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on August 22nd, 2022.</div>
<p>Email validation in PHP can be done in different ways. In general, a website will have <a href="https://phppot.com/jquery/jquery-form-validation-with-tooltip/">client-side validation</a>.</p>
<p>I prefer both client and <a href="https://phppot.com/php/php-form-validation/">server-side validation</a>. It is applicable not only for emails but also to all inputs received from the end users. It will make the website more robust.</p>
<p>Validating email or other user inputs is a basic precautionary step before processing.</p>
<p>In this article, we will see how to validate email on the server-side. PHP provides various alternates to validate email with its built-in constants and functions. Some of them are listed below.</p>
<h2>Ways to validate email in PHP</h2>
<ol>
<li>Using FILTER_VALIDATE_EMAIL.</li>
<li>Using pattern matching with regular expression.</li>
<li>By validating the domain name from the email address.</li>
</ol>
<p>The following quick example uses <a href="https://www.php.net/manual/en/filter.filters.validate.php" target="_blank" rel="noopener">PHP filter</a> FILTER_VALIDATE_EMAIL. It is the <strong>best method of validating email in PHP</strong>.</p>
<div class="post-section-highlight" readability="35">
<h2>Quick example</h2>
<pre class="prettyprint"><code class="language-php">&lt;?php
$email = '[email protected]';
isValidEmail($email); // using FILTER_VALIDATE_EMAIL - this is the best option to use in PHP
function isValidEmail($email)
{ return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
?&gt;
</code></pre>
</div>
<p><img loading="lazy" class="alignnone size-large wp-image-19057" src="https://phppot.com/wp-content/uploads/2022/08/php-validate-email-550x432.jpg" alt="php validate email" width="550" height="432" srcset="https://phppot.com/wp-content/uploads/2022/08/php-validate-email-550x432.jpg 550w, https://phppot.com/wp-content/uploads/20...00x235.jpg 300w, https://phppot.com/wp-content/uploads/20...68x603.jpg 768w, https://phppot.com/wp-content/uploads/20...-email.jpg 803w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p>Let us see other methods to validate email using PHP script.</p>
<h2>Using FILTER_SANITIZE_EMAIL for input sanitization</h2>
<p>The FILTER_SANITIZE_EMAIL is used to clean the email data submitted by the user.</p>
<p>In this example, the $email is hard coded with an example email address. You can supply email input from the form data posted to PHP using <a href="https://phppot.com/php/php-request-methods/">GET or POST methods</a>.</p>
<p>It adds a prior step to sanitize the email data before validating its format. For validating the format of the sanitized email data, it uses FILTER_VALIDATE_EMAIL.</p>
<p class="code-heading">filter-sanitize-email.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
// this script explains the difference between FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL // validation is to test if an email is in valid email format and FILTER_VALIDATE_EMAIL should be used.
// sanitization is to clean an user input before using it in the program and FILTER_SANITIZE_EMAIL should be used.
$email = "[email protected]";
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL); // after sanitization use the email and check for valid email or not
if (filter_var($cleanEmail, FILTER_VALIDATE_EMAIL)) { // the email is valid and use it
}
?&gt;
</code></pre>
<h2>Using pattern matching with regular expression</h2>
<p>If you are expecting a regex pattern to validate the email format, this example code will help.</p>
<p>This code has a regex pattern in a variable and is used to validate email with PHP preg_match().</p>
<p>In PHP, the preg_match() is for pattern matching with a given subject that is an email address here.</p>
<p>This code has the validateWithRegex() function to process the PHP email validation. It applies converts the input email string to lower case and trims before applying preg_match().</p>
<p>Then, it returns a boolean true if the match is found for the email regex pattern.</p>
<p class="code-heading">email-regex.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php
validateWithRegex($email); // Using regular expression (regex). If for some reason you want to validate email via a regex use this
// function. The best way to validate is via FILTER_VALIDATE_EMAIL only.
function validateWithRegex($email)
{ $email = trim(strtolower($email)); // the regex I have used is from PHP version 8.1.7 which is used in php_filter_validate_email // reference: https://github.com/php/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[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?Sad?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iDu'; if (preg_match($emailRegex, $email) === 1) { return true; } else { return false; }
}
?&gt;
</code></pre>
<h2>By validating the domain name from the email string</h2>
<p>This method is a very simple one for validating email. It validates email by ensuring its host DNS validness.</p>
<p>It uses the PHP checkdnsrr() function to validate DNS by a hostname or the <a href="https://phppot.com/php/how-to-get-the-client-user-ip-address-in-php/">site IP address</a>.</p>
<p>This function returns a boolean true if the host has any DNS records found. And thereby, the email in that host can be considered in a valid format.</p>
<p>It follows the below list of steps.</p>
<ol>
<li>It extracts the domain name from the email.</li>
<li>It extracts the username prefixed before the @ symbol.</li>
<li>It ensures that the username and domain name are not empty.</li>
<li>It checks if the DNS details are not empty about the host extracted from the input email.</li>
</ol>
<p class="code-heading">domain-validate-email.php</p>
<pre class="prettyprint"><code class="language-php">&lt;?php // simplest custom email validation using email's domain validation
function validateEmail($email)
{ $isEmailValid = FALSE; if (! empty($email)) { $domain = ltrim(stristr($email, '@'), '@') . '.'; $user = stristr($email, '@', TRUE); // validate email's domain using DNS if (! empty($user) &amp;&amp; ! empty($domain) &amp;&amp; checkdnsrr($domain)) { $isEmailValid = TRUE; } } return $isEmailValid;
}
?&gt;
</code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/php-validate-email.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-validate-email/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/08/...and-regex/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016