-1

I have a register script and i want to validate that people have typed in their email correctly, but this doesnt seem to work? any ideass?

if ($_POST){
$email=stripslashes($_POST['email']);
$valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
if (!$email && !$valid){
    $_SESSION['error'] = "I'm sorry but the email you specified is not valid.     Please enter a valid email";
    unset($email);
    header("Location: register.php");
    exit;

}
    }
neeko
  • 1,930
  • 8
  • 44
  • 67
  • 2
    i assume you didn't write that but copied it from somewhere, you need to makes sure the source of any code is up to date. –  Oct 06 '12 at 01:39
  • sorry i am learning php, i find it easier to copy from other sources and then figure out what makes them work, but yes this has been a trouble that a lot of sources are outdated! – neeko Oct 06 '12 at 01:45
  • The `eregi` function has been deprecated as of PHP 5.3 and I would not recommend using it again. Also, this question has already been answered at: http://stackoverflow.com/questions/1374881/how-to-change-phps-eregi-to-preg-match/1374904#1374904. Please check that answer. – Aamir Oct 06 '12 at 01:32

1 Answers1

7

The right way to validate email in PHP is using the filter_var() function with the constant FILTER_VALIDATE_EMAIL.

filter_var($email, FILTER_VALIDATE_EMAIL)

This will return true if the submitted email is well shaped, otherwise it will return false. You can read more about filters at the Filter Reference.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252