echo "devil";

Email validation step by step

01.07.2010 10:39 | Regular Expressions

This is a detailed step-by-step explanation of a regular expression validating an email address. In this test we use the PHP function preg_match to do the validating (executing the regular expression).

Our test case looks like this:

// Regular Expression
$pattern = "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/";

// Checking email
if( preg_match( $pattern, "" ) ) {
  return true; // Email is valid

Let's start analyzing the regular expression. Since we use preg_match we need to begin and end our pattern with the / delimiter. Let's go from left towards right:

^ - Start of string signal - The string must start with one of the following characters
a-zA-z0-9 - The allowed characters (it means the same in all places of the pattern)
+ - Match 1 or more times the following characters in ()
* - Match 0 or more times the following character - in this case the @
$ - End of string signal - The string must end with one the following characters

Note: This regular expression is not perfect for validating an email address. For more info on this topic I refer to a previous post called Validating an email address using filter_var instead of regex

Tags: email, validation, regex

« Back «

Share |

RSS feed