Tutorial How to Validate Email Addresses with PHP

| | Tutorial PHP |

Tutorial How to Validate Email Addresses with PHP | To ensure the email address entered by the user is correct is one of the points that must be done by a good web developer that the data obtained can not be wrong and useless.

For example in the case of online registration of examinees, when the register and pay a registration fee participants will also be asked to enter your email so that participants can get notifications of time, location and more complete examination schedule via email. However, if the email entered is incorrect, there is a possibility he will fail the exam because it never received an email notification regarding the test. It is certainly detrimental.

Therefore it is very important to make sure an email is valid or not valid, if valid, give an error message and the process stalled until the user to enter an email correctly.

Tutorial How to Validate Email Addresses with PHP

We can do that by using PHP, there are at least two ways that we can apply to validate email addresses using PHP.

Method 1: Validate your email address using filter_var function ()
This is the easiest way to validate an email address with PHP. This method uses the PHP function filter_var () is a very simple and safe to check if an email address is valid or not.

One drawback is that this method can only work on the version of PHP> = v5.2.0

Here is one example of how to use the function filter_var () to validate the email address.
function validate_email($email=NULL) {
    return (filter_var($email, FILTER_VALIDATE_EMAIL) ? "$email is a valid email" : "$email is invalid email");

echo validate_email("exampleemail@gmail.com");
echo "<br/>" . validate_email("exampleemail.gmail.com"); //an example of an email address with no signs @

// output
// exampleemail@gmail.com is a valid email
// user.example.com is an invalid email

Method 2: Validate your email address using RegEx in PHP
If you are still using PHP below version 5.2.0, then you must use RegEx this method to validate an email address. Here are the PHP function to check your email address is valid or not using RegEx expressions.
function validate_email($email=NULL) {
    return (preg_match("/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/",$email) ? "$email
is a valid email" : "$email is an invalid email");

echo validate_email("example.email@gmail.com");
echo "<br/>" . validate_email("user_example@yahoo"); // Example email address is incorrect because it uses characters underscore _

// output
// example.email@gmail.com is a valid email
// user_example@yahoo is an invalid email
well, that's the tutorial how to validate the email address with PHP, may be useful

Illustration email address (Image: addthis.com)

Disclaimer: Images, Content of articles or videos that exist on the web sometimes come from various sources of other media. Copyright is fully owned by the source. If there is a problem with this matter, you can contact us here.
How to style text in Disqus comments:
  • To write a bold letter please use <strong></strong> or <b></b>.
  • To write a italic letter please use <em></em> or <i></i>.
  • To write a underline letter please use <u></u>.
  • To write a strikethrought letter please use <strike></strike>.
  • To write HTML code, please use <code></code> or <pre></pre> or <pre><code></code></pre>, and please parse the code in the parser box below.
Show Parser Box