Struggling to create a function to validate any email address using the script below
if (!preg_match('/^(?=^.{6,64}$)[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $user_mail)) {
$error_mail = empty_mail;
$display_form = TRUE;
$validation_error = TRUE;
} else {
$domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', '', $user_mail);
if (!checkdnsrr($domain)) {
$error_mail = empty_mail;
$display_form = TRUE;
$validation_error = TRUE;
}
}
Server-side validation:
It is a good idea to always validate data server-side. Of course you need to do this client site with JavaScript but you can never trust client validation because you can easily pass that.
That said, hereby the simple PHP server-side e-mail address validation:
//Will return a boolean
filter_var('bob@example.com', FILTER_VALIDATE_EMAIL);
Documentation: http://php.net/manual/en/function.filter-var.php
Define the function and call the function with parameters like this
<?php
function validate_email($user_mail)
{
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
if (preg_match($pattern, $user_mail)) {
return "valid email format";
}
else
{
return "Invalid email format";
}
}
echo validate_email('jothi007@@gmail.com');
?>
OUTPUT :
valid email format