I wrote a regexp a little while ago that will validate an email pretty well. It allows for pretty much anything before the '@' except for whitespace and '@', then allows alphanumerics and hyphens in the domain name (with the same rule for n subdomains), then an alphabetical tld. I figured it would be a waste of time to maintain a list of all the tlds, so I just check to make sure the tld has two or 3 letters, or is 'info' or 'museum'.

/^[^@\s]+@([a-z0-9\-]+\.)+([a-z]{2,3}||info||museum)$/i

I would very much like to know if anyone can offer improvements on this, as I plan to use it again soon, but I've tried quite a number of garbage addresses and they're stopped. A limitation of this regexp is that characters like ';' and ',' are not stopped in the first part, so someone could theoretically send extra addresses -- but the '@' is stopped. So, someone could theoretically get your program to send mail to local accounts, or just generate errors. I felt this was an okay compromise, but to stop this you could change the regexp to:

/^[^@\s;,]+@([a-z0-9\-]+\.)+([a-z]{2,3}||info||museum)$/i

Of course, if you really want to validate the address, you could always do a DNS lookup on the domain name (don't laugh, I know people who've done it), but I can't think of any way to check if the address is valid...


LAI
:eof

In reply to Re: Re: Getting Email environment address by LAI
in thread Getting Email environment address by Anonymous Monk

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.