eMail-Adressvalidierung mit Perl
Grundsätzlich ist es möglich die Syntax einer eMail-Adresse mit einer RegEx zu prüfen. Aber warum sich den Stress machen, wenn es ein fertiges Modul dafür gibt?
Lediglich der Aufruf ist etwas komisch mit
is_email
und
is_email_rfc822
Folgendes Beispiel ist eine komplette Implementierung, die zeigt wie das Modul angesprochen wird.
#!/usr/bin/perl -w
use 5.004;
use strict;
use English;
use warnings;
use Data::Validate::Email qw(is_email is_email_rfc822);
my @args = @ARGV;
my $i = 1;
my @a = (
'[email protected]', # email-Adressen
'Mickey [email protected]',
'[email protected]',
'[email protected]',
'Mickey;[email protected]',
'[email protected]',
'Mickey!§$%&()[email protected]',
'[email protected]',
'[email protected]',
'Mickey;[email protected]',
'[email protected]',
'A-Za-z0-9.!#$%&\'*+-/=?^_`{|}[email protected]',
'Peter.Mü[email protected]',
'Peter@Müller.de',
'pascal;[email protected]',
'broomstick@hotmaicom',
'xyz',
'[email protected]',
'[email protected]',
'[email protected]',
);
my $header .= "-" x 75 . "\n";
$header .= " Nr.| RFC | regex1| regex2| eMaill-Address \n";
$header .= "-" x 75 . "\n";
print $header;
foreach my $eMailAdr (@a) {
my $is_email = "no";
my $invalid = "no";
my $is_822 = "no";
my $regex1 = "no";
my $regex2 = "no";
chomp $eMailAdr;
if ( is_email($eMailAdr) ) {
#print "$eMailAdr\tLooks like an email address\n";
$is_email = "yes";
}
elsif ( is_email_rfc822($eMailAdr) ) {
#print "$eMailAdr\tDoesn't much look like an email address, but passes rfc822\n";
$is_822 = "yes";
}
else {
#print "$eMailAdr\tNot an email address\n";
$invalid = "yes";
}
$_ = $eMailAdr;
if (m/([_a-zA-Z0-9-]+)([\._-]*[a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})/) {
$regex1 = "yes";
}
$_ = $eMailAdr;
if (m/^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[a-zA-Z0-9][A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$/) {
$regex2 = "yes";
}
printf '%3s |%5s |%5s |%5s | %-45s ', $i, $is_email, $regex1, $regex2, $eMailAdr;
print "\n";
$i++;
}






Hinterlasse einen Kommentar
An der Diskussion beteiligen?Hinterlasse uns deinen Kommentar!