Email validation with PHP


Nowadays almost all website has some kind of html form. The most best-known are the user registration forms, information request forms and so on. As the form makes sense only if the visitor submits valid information so the site developer should take care of the data alidity.

The form validation process can be divided into 2 categories. [list]Client side validation*Server side validation[/list]
The client side validation is mostly realized via JavaScript code. The pro is that the processing doesn’t require any network transfer so it can be faster. The con is that the visitor can disable JavaScript in the browser and in this case he/she can submit invalid data.
That’s the point where the server side validation becomes important. The server side scripts can not be influenced by the visitor so you know that it will work in the same manner for all the visitors. The best solution is if you apply both type of validation methods.

[b]Validation technics[/b]

From this point on I will focus only on the server side validation of an email address. In PHP you receive the form fields values in the $_POST or in the $_GET arrays. You must get the actual value to be checked from one of these arrays. When you have the correct variable you can try to run some validation routines on it.
There are again 2 main methods how you can check an email string: [list]String manipulation routines*Regular expressions[/list]

[b]What to check[/b]

To make a good working validation routine you first need to clarify what is allowed and what is not. Let’s see how any email string should look like: [b][/b]
Now analyze this string a little bit. You should check the following points:
[list]The string must contains one and only one ‘@’ character.*Before the ‘@’ it must have at least one character.*After the ‘@’ it must have a valid domain format with at least one ‘.’*The email can not contain any invalid character.*The total length of an email should be at least 6 character (*[/list]
[b]Regular expressions[/b]

If you try to create a checker function with string manipulation routines such as strlen, strpos,… then it will result in a quite complicated if-else condition structure.
An other solution is using regular expressions. In this case you should define a pattern which should fit on the relevant string. For example: pattern is '^Test' and the subject is 'Test string'. This will pass as the pattern is valid for all strings which begins with the substring 'Test'.
The pattern for the email validation is a little bit more complicated. [code]^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})[/code]
Hoop’s it is really quite complex. I try to explain it. Let’s divide the pattern into smaller parts. The first part is until the ‘@’ character.
[code]^[_a-z0-9-]+(\.[_a-z0-9-]+)*[/code] Explanation:
-- [b]^[/b] means that we start the check from the first character of the string.
-- [b][_a-z0-9-]+[/b]: There must be at least 1 character between a and z or between 0-9 or ‘_’ .
-- [b](\.[_a-z0-9-]+)*[/b] : The first character group will be followed by 0 or more character groups which always begins with a ‘.’
Now try to interpret the second part: [code]@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$[/code] Explanation:
-- The ‘[b]@[/b]’ means that after the first part this character is mandatory exactly once.
-- [b][_a-z0-9-]+[/b]: As before
-- [b](\.[_a-z0-9-]+)\*[/b]: As before
-- [b](\.[a-z]{2,3})$[/b]: This means that at the end of the email string there must be a 2 or 3 character long substring. And before it a ‘.’ is mandatory.

[b]Implement in PHP[/b]

PHP has some built in function to support regular expressions. Now we just use one of them the eregi.

[b]Function[/b] : eregi - case insensitive regular expression match [b]Usage[/b] : bool eregi ( string pattern, string string [, array regs]);
A new function was created to test the email string whether it is valid or not. Later you can just call this function. It returns true if the email is valid and false otherwise. Let’s see the code: [code]<?php // This function tests whether the email address is valid function isValidEmail($email){ $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; if (eregi($pattern, $email)){ return true; } else { return false; } } ?>[/code] It' quite easy, isn't it? Now we have the validation code. Let’s make a small test form to see it in action: <?php    // This function tests whether the email address is valid      function isValidEmail($email){       $pattern "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";             if (eregi($pattern$email)){          return true;       }       else {          return false;       }       } ?>    <head>   <title>Email validation form</title> </head> <body>   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="emailForm">      <table>        <tr><td>Email:<input name="email"></td><tr>        <?php          if (isset($_POST['submitemail']))          {             if (isValidEmail($_POST['email'])){                 echo "<tr><td>The email: ".$_POST['email']." is valid!</td></tr>";             }             else{                 echo "<tr><td>The email: ".$_POST['email']." is invalid!</td></tr>";             }          }        ?>        <tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>      </table>   </form> </body>