3

I have problems to validate an email adress using preg_match().

This is the block of code i have.

if (VerifierAdresseMail($email))
  echo '<p>valide.</p>';
else
  echo '<p>not valid</p>';


  function VerifierAdresseMail($adresse) {
    $Syntaxe = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#';
    if (preg_match($Syntaxe, $adresse))
      return true;
    else
      return false;
  }

this does not seem to work. it's just returning a white page. I can't see the error althought i have this in my application.ini

[developpement : production]


phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpSettings.track_errors = 1
phpSettings.error_reporting = E_ALL

resources.frontController.params.displayExceptions = 1

phpSettings.date.timezone = "Europe/Paris"

Thanks for your help

2 Answers2

21

Do not use preg_match() to validate an email address. Use filter_var() instead:

if (filter_var($adresse, FILTER_VALIDATE_EMAIL)) {
     // valid
} else {
     // not valid
}
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
3

I think you should rather use this :

filter_var('e-mail@provider.com', FILTER_VALIDATE_EMAIL);

Returns the filtered data, or FALSE if the filter fails.

doc: http://php.net/manual/en/function.filter-var.php

Olivier Kaisin
  • 519
  • 3
  • 12