Verifying mail address format

Top Japanese page




Overview

Verify mail address format. Check if the input matchs to somehting like abc-cde.fg@hijk-lmn.com.

Flow

  1. Check mail address using patter match

A sample code

 my $mail_pattern = '^[\w\.\-]+\@[\w\.\-]+\.[a-zA-Z]{2,4}$';
 
 if ($mailaddress !~ /$mail_pattern/){
     print 'Invalid mail address';
     exit;
 }

Descriptoin of the code

 my $mail_pattern = '^[\w\.\-]+\@[\w\.\-]+\.[a-zA-Z]{2,4}$';

Put the mail address pattern into a variable. This is not absolutely necessary. If multiple checks will occur later on, this should be done just for convenience.

 if ($mailaddress !~ /$mail_pattern/){
     print 'Invalid Mail address';
     exit;
 }

If the address in $mailaddress does not match to $mali_patter, display an error massage.