Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Validate an email address (See related posts)

I don't remember where this came from, but it's very useful for validating input from a form


function valid_email($email) {

  if( eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) ) { 
    return true;
  }
  else { 
    return false;
  }

}


Comments on this post

Rot posts on Feb 24, 2006 at 15:44
As so often is the case - the regular expression is too simplistic to be a real measure of complience.

Never mind whether or not an address that meets the RFC acutally exists.
Jeff posts on May 02, 2006 at 02:24
Also, why did you use an evaluate a boolean, and use it in an if, rather than simply returning that. Look:

function valid_email($email) {
  return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) );
}


Of course, this still isn't great validation, as Rot pointed out, but this does simplify it, at least.
Jeff posts on May 02, 2006 at 02:25
I think I put the wrong number of parentheses, but you get the point.

You need to create an account or log in to post comments to this site.


Related Posts