Home > php > Validate an e-mail address in PHP [PHP]

Validate an e-mail address in PHP [PHP]

Problem

You have an input form with an “e-mail address” field and you want to validate the user input. You want a simple solution that checks if the e-mail is well-formed or not.

Solution

// from wordpress code: wp-includes/formatting.php
function is_email($user_email) 
{
	$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";

	if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) 
	{
		if (preg_match($chars, $user_email)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

Credits

I found this solution here in a comment.

Categories: php Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: