Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Checking on multiple email addresses in a string

by c (Hermit)
on Jul 22, 2001 at 18:34 UTC ( [id://98822]=perlquestion: print w/replies, xml ) Need Help??

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

I've written a control panel of sorts which will allow users to update the list of email aliases they have set up for their domain. In this current version of the panel, each alias can have only one true destination email address rather than multiple recipients.
As a result of this, until I add that feature :-) I need to double check to make sure that end users do not input more than one alias for the valid destination email.
I am using the following regex to check a formfield value to see if what appears to be more than one email addr exists in its value:

} elsif { ( $add_destination =~ /^(\w|\.|\-)+\@(\w)+\.(\w)+(.|..)(\w) ++\@/ )

It seems poorly formed to me, but works. I am not really certain why i need the or operator within my first grouping, but trying (\w\.\-)+ doesnt seem to work even though I use a similar regex, [^\w\.\-] to test for illegal characters within an email address.

It might be easier to just check for more than one \@ but I am not sure how to say one is okay, but two is not within regex. The above regex maps out an email address followed by one or two characters including the possibility of whitespace followed by the beginning of another email address which will include another \@
I know that a couple modules like Email::Valid could be my solution for checking for illegal characters, but I know their must be a better solution for checking for multiple \@ within a string.

humbly -c

Replies are listed 'Best First'.
Re: Checking on multiple email addresses in a string
by wog (Curate) on Jul 22, 2001 at 19:50 UTC
    Email::Valid is probably the best solution for both checking for illegal characters and multiple emails addresses. This module appears to find an address invalid if you try to put multiple addresses there. Alternately you could use Mail::Address and just make sure you get one address out of its parse method. Your regex will not handle many addresses which it is valid to send e-mail to, such as "me, you@example" <me@example.com> or (from alias someaddr@somewhere) me @ example . com or me@[1.2.3.4] or space(@)@example.com (and worse!). Also your idea of looking for more then one @ will certainly not work on addresses like that, either. Nor will checking for comma and/or space.

    That said, character classes are always inclosed by []s. (\w\.\-)+ tries to match a word character followed by a dot followed by a - one or more times. You can fix this problem by using a real character class: [\w.-]+ (note that in character classes the . is not special, and the - is not special at the end or beginning of the character class.)

    You can check for more then one @ with tr: my $count = $addy =~ tr/@//; if ($count > 1) { ... } You could also try a regex like /@[^@]*@/ to check for that.

    update: tr/+/*/ in last regex.

    update: fixed first sentence (to mention multiple e-mails.)

(dkubb) Re: (2) Checking on multiple email addresses in a string
by dkubb (Deacon) on Jul 22, 2001 at 21:58 UTC

    You should check out a module called Email::Find, it will match email addresses in a string, and uses Email::Valid underneath to validate each match.

    Here is some sample code that matches email addresses in a string, and pushes each of them onto an array. All you'll have to do is check to make sure that @MATCHES has only a single element, then you know the user has submitted one email address.

Re: Checking on multiple email addresses in a string
by wine (Scribe) on Jul 22, 2001 at 19:45 UTC
    I guess you user would seperate their addresses with a space or a comma. I would check on that.

    Something like:

    #!/usr/bin/perl -w use strict; sub is_single { my $email = shift; # remove spaces from beginning and end $email =~ s/^\s*|\s*$//g; # check for characters invalid in an emailaddress return $email=~/[\s,]/?0:1; } # prints 1 print is_single('hpotter@hotmail.com'); # prints 0 print is_single('hpotter@hotmail.com, mickymouse@disney.com');

    But I guess some people around here have a far more beautiful solution.

    Update strict-ing the code and making it more verbose

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://98822]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found