How to Check Email Address Format With PHP

How to Check Email Address Format With PHP thumbnail
Making sure an email address is valid can be a tricky process.

One of the most common tasks when creating a website with PHP is accepting and validating email addresses. The easiest way to do this is by using regular expression. While there are many different possible regular expression strings that can be used to check the format of an email address, at least one is simpler than the others.

Instructions

  1. Create a Validation Function

    • 1

      Create the regular expression string that will be used to validate the email addresses. Many different programmers have created many different regular expression strings for this purpose, and you are free to create your own, but one of the simpler versions is listed here:

      "/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/"

    • 2

      Create the validation function. With the regular expression string, you can use preg_match to validate any string and confirm that it is a valid email address. An example function is listed here:

      function validateEmail($email) {

      if (preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {

      //The address is valid

      return true;

      }

      //The address is not valid

      return false;

      }

    • 3

      Use the validation function to validate any user-inputted email addresses.

Tips & Warnings

  • Check out the Regex Library in the "Resources" section for more examples of validation strings.

  • The example validation string in this article is very basic. Some unusual or complicated email address may not be matched by it.

Related Searches:

References

Resources

  • Photo Credit internet image by arabesque from Fotolia.com

Comments

You May Also Like

Related Ads

Featured