wallyweezle has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've been tinkering with this for about an hour now trying to figure out why it won't catch intentional mistakes that I throw in. What I'm doing is trying to validate an email address, with something like this:
if ($MailTo !~ /\@*\./) { #set a kill flag #print a message }
So, to clear up the seemingly random characters up there, that's a / to open it, and escaped @ sign, a *, an escaped ., then another / to close it. Essentially I'm looking for an @ sign then a dot. On the console, this line works great, catches mistakes and everything. But when I run it through a web page as a CGI script it doesn't catch it unless I don't put anything in the field. Any ideas? Here's all of the code, just to see if anybody catches a minor mistake in my code:
use Net::SMTP; use CGI ':standard'; #Get parameters from http string# my $ServerName = param('server'); my $MailFrom = param('from'); my $MailTo = param('to'); my $Subject = param('subject'); my $Content = param('content'); print "Content-type: text/html\n\n"; print "<html>\n <head>\n <title>Mail Results</title>\n </head>\n <body>\n"; my $kill = 0; if (!($MailFrom =~/\@*\./)) { $kill = 1; print "Invalid return address in From box. "; } if (!($MailTo !~/\@*\./)) { $kill = 1; print "Invalid destination address. Please notify the webmaster o +f this problem by sending an email to wvhs-web\@charter.net. "; } if ($kill == 1) { die("One or more errors have occurred which prevent the script fro +m continuing further. "); } else { $smtp = Net::SMTP->new($ServerName, Debug => 0); die "Couldn't connet to server: $!" unless $smtp; $smtp->mail($MailFrom); $smtp->to($MailTo); $smtp->data(); $smtp->datasend("To: $MailTo\n"); $smtp->datasend("From: $MailFrom\n"); $smtp->datasend("Subject: $Subject\n"); $smtp->datasend("\n"); $smtp->datasend("$Content\n\n"); $smtp->dataend(); $smtp->quit(); print "Mail Sent Successfully\n \n"; } print "</body>\n </html>";
Thanks, Eric

edited by ybiC: Balanced <code> and <readmore> tags, retitle from "Regex problem"

Replies are listed 'Best First'.
Re: Regex problem
by edoc (Chaplain) on Jul 31, 2003 at 02:03 UTC

    check out Email::Valid

    if (!($MailTo !~/\@*\./)){ .. }

    you've got a double negative there, so as I read it, it says:

    if $Mailto contains zero or more @'s (\@*) followed by a '.' (\.) then $kill = 1

    or, more simply.. if $Mailto contains a '.' then $kill = 1

    cheers,

    J

Re: Regex problem
by antirice (Priest) on Jul 31, 2003 at 02:04 UTC

    Your regex isn't doing what you want. If you set $MailTo to "bob@@@tom.@blah@." then it will think that you have a fully qualified e-mail address. I would recommend that you use Email::Valid and its rfc822 method.

    To figure out where your regex went wrong, check out perlre for more info.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Regex problem
by graff (Chancellor) on Jul 31, 2003 at 02:21 UTC
    Double negatives in code always confuse me a bit... You have these two conditions:
    if (!($MailFrom =~/\@*\./)) { $kill = 1; print "Invalid return address in From box. "; } if (!($MailTo !~/\@*\./)) { $kill = 1; print "Invalid destination address... "; }
    The following pseudo-code properly "translates" this logic, so the question is: does it also express your true intent?
    • if the "from" parameter string contains at least a single period (dot) character, which optionally follows "@", then it's okay, otherwise, complain about it.
    • if the "to" parameter string does not contain a period, which optionally follows "@", then it's okay, otherwise complain about it.

    Now if the criterion is supposed to be "@" followed by period, you should not have the asterisk after "@" -- this means "zero or more occurrences" of "@" preceding a period, so both "." and "@." match your /\@*\./ regex (but neither of them looks good to me as an email address).

Re: email address validation regexp
by nega (Scribe) on Jul 31, 2003 at 13:10 UTC