Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: PHP
Expertise: Beginner
Oct 19, 2004

Validating Email Addresses with Regular Expressions in PHP

Most Web forms designed in PHP are made for user interaction. This generally means information exchange, and the most common way this is done is via email.

Because of this, validating email addresses for misprints or false entries is crucial for succesful interactions.

The following class is small and very easy to use. It uses regular expressions for validating email adresses.


-------Class------------------------------------------------

class email{

function valid($str)
{
	if (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $str))
	{
		return 1;
	}
	else
       	{
		return 0;
 	}
}
}


---------Example usage---------------------------------------

$check_valid = new email;
$mail="here_is_the_mail@mail.com";

if($check_valid->valid($mail))
{
  echo "<br> Email is valid";
}
else 
{
  echo "<br> Email is not valid";
}

Cem Aykan
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date