Search code examples
phpfunctionstrpos

PHP strpos check mail on @ and


I made a little PHP script that checks if an email is valid. The only problem is that it doesn't check if the dot is BEHIND the '@'. It accept emails like this: Hi.Hello@hotmailcom when it should only accept emails like HiHello@hotmail.com

This is my script:

<?php
$mail = $_POST['mail'];

    function checkmail($mail)
        {
            if ((strpos ($mail, '@') !== false) && (strpos ($mail, ".") !==false))
            {                   
                return true;
            }
            else
            {
                return false;
            }
        }

if(checkmail($mail))    
{
echo"Goed"; 
}   
else    
{       
echo"Fout";     
}

?>

Thanks in advance!


Solution

  • Don't badly reinvent the wheel, use filter_var('bob@example.com', FILTER_VALIDATE_EMAIL), or in your case better filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL).

    http://php.net/filter_var
    http://php.net/filter_input