PHP – Validate Email Id :
The below example will show you how to validate email id in PHP
$mailId = “email@example.com“; // Valid email
$conditions = ‘/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([comco.in])+$/’;
if (preg_match($conditions, $mailId))
echo “Valid Mail Id”;
echo “Invalid Mail Id”;
In the above example “$mailId” having the email id given by user. and “$conditions” having conditions for validate given email id. here i used “preg_match()” predefined php function. this function return boolean value. if the match occurred then it will return true otherwise false.
The above example print “Valid Mail Id”. Here the variable “$conditions” have “com”,”co”,”in” conditions only, so it will allow these extension mail id only. and it will validate all allowed special characters only.
if A$mailId=”firstname.lastname@example.org” then the output is “Invalid Mail Id”.