0

Possible Duplicate:
Is there a php library for email address validation?

How I can write php script that test e-mail address is input correctly and verify that the input begins with a series of character followed by the @ character, another series of character and a final series of characters.

Community
  • 1
  • 1
eslame
  • 1
  • 2

4 Answers4

3

The filter_var() function, using the FILTER_VALIDATE_EMAIL filter, should do exactly what you want -- no need to re-invent the wheel ;-)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

Use the PHP function filter_var() with the FILTER_VALIDATE_EMAIL flag to validate the email address:

$emailValid = filter_var($email, FILTER_VALIDATE_EMAIL);

if($emailValid) {
  echo "Email is valid";
} else {
  echo "Email is INVALID";
}
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
2

I mostly use filter_var for this, but a fellow github'r notified me that this function is flawed.

He recommended to use the rather more complex validator at http://www.dominicsayers.com/isemail/.

Good luck!

Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
0

if(preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$email){
      $inputcorrectly = true; // Or whatever
}

Will Manson
  • 167
  • 1
  • 4
  • 12