Trying to catch invalid emails

Discussion in 'Perl Misc' started by Samik R., May 7, 2008.

  1. Samik R.

    Samik R. Guest

    Hello,
    I use the following regular expression to catch typical invalid email
    addresses:
    ------------
    my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");
    foreach (@Email)
    {

    if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
    { print "$_ is a valid email id\n"; }
    else
    { print "$_ is an invalid email id\n"; }
    }
    -------------

    This expression does not catch the above 3 emails in the array (the
    program says that they are valid emails).

    Can someone help me to discard these three?
    Thanks.
    Samik R., May 7, 2008
    #1
    1. Advertising

  2. Samik R.

    Ron Bergin Guest

    On May 6, 7:58 pm, "Samik R." <> wrote:
    > Hello,
    > I use the following regular expression to catch typical invalid email
    > addresses:
    > ------------
    > my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");
    > foreach (@Email)
    > {
    >
    > if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
    > { print "$_ is a valid email id\n"; }
    > else
    > { print "$_ is an invalid email id\n"; }}
    >
    > -------------
    >
    > This expression does not catch the above 3 emails in the array (the
    > program says that they are valid emails).
    >
    > Can someone help me to discard these three?
    > Thanks.


    Try using the Email::Valid module.

    use strict;
    use warnings;
    use Email::Valid;

    my @Email=("sam._\@abc.org", "sam_.\@abc.org", "sam_.\@abc.org");

    foreach my $address (@Email)
    {
    print Email::Valid->address($address) ? "$address valid\n" :
    "$address not valid\n";
    }
    Ron Bergin, May 7, 2008
    #2
    1. Advertising

  3. Samik R.

    Ben Morrow Guest

    Quoth "Samik R." <>:
    > Hello,
    > I use the following regular expression to catch typical invalid email
    > addresses:


    Don't do that. Use a module, such as Email::Valid, that actually gets it
    right.

    Ben
    Ben Morrow, May 7, 2008
    #3
  4. Samik R.

    Ben Bullock Guest

    Samik R. <> wrote:

    > if(/^[A-z0-9]+([_\.][A-z0-9\-]+)* ...


    The problem is A-z here, A-z contains all of

    ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz

    so it matches the underscore in the email address.

    To match only letters, you should use [A-Za-z0-9] instead.
    Ben Bullock, May 7, 2008
    #4
  5. Samik R.

    Gordon Etly Guest

    Abigail wrote:
    > _
    > Frank Seitz () wrote on VCCCLXIII September
    > MCMXCIII in <URL:news:>:
    > ;; Abigail wrote:
    > ;; > _
    > ;; > Samik R. () wrote on VCCCLXIII September
    > :: > :) MCMXCIII in ;; > <URL:news:fvr60f$pqd$>:
    > ;; > :) Hello,
    > ;; > :) I use the following regular expression to catch typical
    > :: > :) invalid email ;; > :) addresses:
    > ;; > :) ------------
    > ;; > :) my @Email=("sam._\@abc.org", "sam_.\@abc.org",
    > ;; > :) "sam_.\@abc.org"); ;; > :) foreach (@Email)
    > ;; > :) {
    > ;; > :)
    > ;; > :)
    > ;; > :)
    > if(/^[A-z0-9]+([_\.][A-z0-9\-]+)*[@][A-z0-9_\-]+([.][A-z0-9_\-]+)?\.[A-z]{2,3}$/)
    > ;; > :) { print "$_ is a valid email id\n"; } ;; > :) else
    > ;; > :) { print "$_ is an invalid email id\n"; }
    > ;; > :) }
    > ;; > :) -------------
    > ;; > :)
    > ;; > :) This expression does not catch the above 3 emails in the
    > ;; > :) array (the ;; > :) program says that they are valid emails).
    > ;; > :)
    > ;; > :) Can someone help me to discard these three?
    > ;; >
    > ;; > Sure.
    > ;; >
    > ;; > if (/_/) {
    > ;; > print "$_ is invalid";
    > ;; > }
    > ;; > else {
    > ;; > print "$_ is valid";
    > ;; > }
    > ;;
    > ;; An e-mail address with _ (or ._ or _.) isn't invalid, AFAIK.
    >
    >
    > But that wasn't what he asked.
    >
    > He wanted to filter out three particular email addresses.


    Oh please, you know as well as most everyone else those were just
    samples. In any case, flagging an email as invalid for having an
    underscore ( /_/ ) is plain wrong, and portraying it as a solution is
    disingenuous to the OP as well as anyone who comes by this in the
    archives.

    --
    G.Etly
    Gordon Etly, May 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,137
    Xenos
    Aug 20, 2004
  2. Adam
    Replies:
    9
    Views:
    552
    red floyd
    Feb 2, 2006
  3. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    548
    Daniel Pitts
    Aug 5, 2009
  4. Chris Hall
    Replies:
    2
    Views:
    213
    Chris Hall
    Sep 30, 2006
  5. anonymous
    Replies:
    6
    Views:
    94
    gnari
    Feb 29, 2004
Loading...

Share This Page