if else validate email code example

Example 1: regex to validate email

# Python
r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"

Example 2: validate email function

# Language: Perl

sub Validate_Email($)
{
   my $sEmail = $_[0];
   my $sRetMsg = "";

   my $sUserNmRegex = "^[[:alnum:]]+([.!#\$\%&'*+-\/=?^_'{|]?[[:alnum:]]+)*";
   my $sDomainRegex = "@[[:alnum:]]+([.-]{1}[[:alnum:]]+)*";
   my $sEndRegex = "([.]{1}[[:alnum:]]+)+";

   #  Work
   #--------#

   if ($sEmail =~ /$sUserNmRegex$sDomainRegex$sEndRegex$/) {
      $sRetMsg = "Email is valid";
   }
   else {
      $sRetMsg = "Email is not valid";
   }

   return $sRetMsg;
}

my $sEmail = '[email protected]';
print "[Email:$sEmail] : " . Validate_Email($sEmail) . "\n";

# OUTPUT -> [Email:[email protected]] : Email is valid