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.)


In reply to Re: Checking on multiple email addresses in a string by wog
in thread Checking on multiple email addresses in a string by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.