Last updated on January 4th, 2022 at 11:48 am

The below function will valide the email address,

Function to validate email address in PHP

function validateEmail($email)
{
   if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
      return true;
   else
      return false;
}

Detailed explanation of the regex above.

^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$

The ^ sign represents the beginning of the string.

[a-zA-Z0-9._-]

a-z Matches character range “a” to “z”
A-Z Matches character range “A” to “Z”
0-9 Matches character range “0” to “9”
. Matches character “.”
_ Matches character “_”
– Matches character “-”

+

Match one of more preceding charecter

@

Matches a character @

[a-zA-Z0-9-]

a-z Matches character range “a” to “z”
A-Z Matches character range “A” to “Z”
0-9 Matches character range “0” to “9”
– Matches character –

+

Match one of more preceding charecter

\.

Escape character, Matches character “.”

[a-zA-Z]

a-z Matches character range “a” to “z”
A-Z Matches character range “A” to “Z”

{2,3}

Matches between 2 and 4 preceding charecter

?

Matches between 0 and 1 preceding charecter

Group 1

\.

Escape character, Matches character “.”

[a-zA-Z]{2,3}

Matches any character range “a” to “z” (Case Sensitive) between 2 and 3

Group 2

Same as group 1 above.

Note: The last part(Group) is optional when you have an email with domain for example [email protected] or something

Leave a Reply

Your email address will not be published. Required fields are marked *