E-mail validation in PHP (Simple function w/ regex)

sandykadam

202.***.***.***
1,172 days ago

E-mail validation in PHP (Simple function w/ regex)

PHP code:


<?php
// Function to validate an email address

function emailValidate($Email)
{
if (preg_match("/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/", $Email))
{
return 1;
}
else
{
return 0;
}
}

// Example usage - displays the appropriate message depending on whether the e-mail address is valid.

$email = &#8220;sandy@example.com&#8221;;

if(emailValidate($email))
{
echo 'The given e-mail address is valid.';
}
else
{
echo 'The given e-mail address is NOT valid.';
}

?>