email validation in php

function filter_var(), which takes two parameters as input email and second is type of email filter. This function filters the email and returns true or false.

The FILTER_VALIDATE_EMAIL filter validates an e-mail address.


code:-



function validateEmali($email)
{
    if(empty($email))
    {
        $email_msg="please input email address";
   return $data=array("msg"=>$email_msg);
    }
    else{
        $data=array();
        if(filter_var($email, FILTER_VALIDATE_EMAIL))
         {
             $email_msg="valid email address";
             $data=array("email"=>$email,"msg"=>$email_msg);
             return $data;
         }
         else{
             $email_msg="invalid email address";
             $data=array("msg"=>$email_msg);
             return $data;
           }
    }
   
}
validateEmail("code@gmail.com");

output:-

it return array you can access using key.

Array
(
    [email] => code@gmail.com
    [msg] => valid email address
)



Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post