Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

About validating mail id

by pavanmach (Novice)
on Feb 03, 2010 at 12:10 UTC ( [id://821136]=perlquestion: print w/replies, xml ) Need Help??

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

hi, I am using the following regular expressions for validating a mail id
my $mail_id = 'data@mach.com.com'; if($mail_id=~/[A-Za-z0-9._%-]\@[A-Za-z0-9_%-.].[com]/) { print "matched \n"; } else { print "Not matched \n"; }
also i have used this regular expression:
if($mail_id=~/^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$/) { print "matched \n"; } else { print "Not matched \n"; }
But unforunately both of them are matching .com.com I would like to match excatly one .com . How can this be accomplished. Any help would be grately appreciated. Thanks & Regards, Pavan

Replies are listed 'Best First'.
Re: About validating mail id
by Corion (Patriarch) on Feb 03, 2010 at 12:14 UTC

    Why? http://com.com is a valid web address and I suppose they also accept mail, so data@com.com could be a valid email address as could data@mach.com.com.

    The best way to validate an email address is to send mail to it and have the recipient click a link in the mail.

      data@com.com could be a valid email address as could data@mach.com.com

      Neither com.com nor mach.com.com have a valid MX record, and the A record IP for these domains does not accept an SMTP connection, so at the moment they are not valid email addresses. Unless, of course, a local mail server rewrites these addresses into something different, or someone turns on the mail server at those addresses in five minutes, in which case they most certainly are :-).

      I agree completely with the general point that these are "syntactically" correct email addresses and that testing for validity in the way the OP is doing is next to useless. For a more useful approach, he should see Email::Valid. For a regular expression that checks the syntactic validity of an email, check out the Mastering Regular Expressions book (warning, IIRC the regex has approximately the length of an entire page of printed text).


      All dogma is stupid.
        Neither com.com nor mach.com.com have a valid MX record, and the A record IP for these domains does not accept an SMTP connection, so at the moment they are not valid email addresses. Unless, of course, a local mail server rewrites these addresses into something different, or someone turns on the mail server at those addresses in five minutes, in which case they most certainly are :-)
        Just because an MX record isn't propagated to you doesn't mean it doesn't exists. Many companies and organisations have a DNS that looks differently internally than externally. Heck, even my local network consisting of a whopping 3 machines has an internal DNS different from the outside world.

        It's not possible to determine a given address exists. What you can do is check whether a given address is syntactically valid (surprisingly many strings are valid addresses - a valid address doesn't have to contain a '@' - this posting will contain many, many valid addresses). But it doesn't mean that an address that is syntactically (according to RFC 822 or any of its successors) invalid isn't deliverable.

        Another alternative is to send a message to see what happens. If you get a reply that looks like it was written by a human, it's probably a valid address. (Of course, it may have become invalid after the reply was send....). If you get a bounce, it may be invalid (but I can bounce messages from mutt).

        ucbvax!ibm4java!dec4javafan!javafan

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: About validating mail id
by FunkyMonk (Chancellor) on Feb 03, 2010 at 13:12 UTC
    Your regex doesn't come close to matching valid email addresses:

    [A-Za-z0-9._%-] matches one letter, digit, dot, underscore, percent or dash character
    \@ an "at" sign
    [A-Za-z0-9_%-.] matches one letter, digit, underscore, percent, dash or dot character. Update: it gets worse: %-. is a range, so any one of '%&\'()*+,-.' would also match
    . any single character except a newline
    [com] matches the letter 'c', 'o' or 'm'

    But unforunately both of them are matching .com.com

    It matches much more than that:

    /[A-Za-z0-9._%-]\@[A-Za-z0-9_%-.].[com]/ and say "'$_' matched" for 'x@zzc', 'a b c@c!c', '?@?@?e@a.m'; __END__ 'x@zzc' matched 'a b c@c!c' matched '?@?@?e@a.m' matched

    As others have said, the only way to truly validate an email address is to send mail to it, and hope the owner replies.

    Update: updated the second character class

Re: About validating mail id
by scorpio17 (Canon) on Feb 03, 2010 at 14:38 UTC
    I highly recommend Email::Valid for this sort of thing (verifying the form of an email address against the published standards).
Re: About validating mail id
by ww (Archbishop) on Feb 03, 2010 at 12:33 UTC
    If your issue is "validating" in the sense of determining whether an email address found somewhere or supplied by some user is actually in use, Corion's advice covers the waterfront. Just because an address satisfies the relevant standards doesn't mean there is actually such an address.

    OTOH, if your question is more about regexen, see "looking ahead" in perlretut.

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found