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

I need to make a check to make sure an Email was entered in the "From" part of my form correctly such as "myemail@hotmail.com". My check catches bad entries like "myemail@" or "myemail," but seem to have a problem with something like this: "myemail@hotmailcom". Please advise a better regular expression to catch a bad email entry.

This is what I have now:
unless ($email =~ /\w+@\w+.\w+/) { print "Bad Email entry\n"; }

Replies are listed 'Best First'.
Re: Checking Email by reg expression
by broquaint (Abbot) on Nov 01, 2002 at 14:04 UTC
    Seriously, don't roll your own regex for checking the validity of an e-mail address, it is *far* more complex than it looks (just check out the rfc). Just use a module to do the dirty work for you like Email::Valid.
    HTH

    _________
    broquaint

Re: Checking Email by reg expression
by davis (Vicar) on Nov 01, 2002 at 14:07 UTC

    You could do it with a regular expression: see Jeffrey Friedl's book for the example. The trouble with this is that RFC 822 allows a rather large number of different formats - the example mentioned above is 6598 bytes long (Appendix B).

    I'd suggest that using a regex is a very bad idea, and I would use something along the lines of Email::Valid to do it for me. If you don't want to use modules (why?), then follow the advice revealed by typing

    perldoc -q "How do I check a valid mail address?"
    at your command prompt. (Adjust for your OS and perl installation.)
    cheers
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re: Checking Email by reg expression
by peschkaj (Pilgrim) on Nov 01, 2002 at 14:15 UTC

    It's good taht you asked this before trying it. For starters, you would need to escape that . in your regex.

    Second, I'm going to agree with broquaint and davis on this. Your regex will most likely bomb on my email and the email addressses of my co-workers. All of us are first.last@ but the domain changes. Some are company.com some are sudomain.company.com. You can see how this can get crazy very quickly.

    Be lazy, download a module. It spares you pain.

    If you make something idiot-proof, eventually someone will make a better idiot.
    I am that better idiot.
      Thanks for all info.
      I am using a Unix server that I can not download this module to so I was hoping to just verify my company only emails which are always "myemail@here.com"
      If someone can assist with what I have I would appreciate it.
        I am using a Unix server that I can not download this module to so I was hoping to just verify my company only emails which are always "myemail@here.com" If someone can assist with what I have I would appreciate it.
        If you can download your script, you can download the module. It does not have to be installed in the Perl directory tree. That said, you can check the specific mail addresses as /^\w+\@here\.com$/
Re: Checking Email by reg expression
by hacker (Priest) on Nov 01, 2002 at 14:39 UTC
    As I've explained before, in a similar thread, there is only one way to validate an email address..

       Send the person and email and see if they respond.

    Take a look at the other modules mentioned in that writeup for some other clues.