Solved

How to check an Email address?

Posted on 1998-10-06
5
127 Views
Last Modified: 2010-03-05
I'm not realy confident with expression in Perl to check the validty of string. I would like to check the "validty" (presence of the '@' followed by one or several '.') of
an Email address provided by a user.

What should the expression look like?
if ($email =~ /\@+\.*/) does not match my request because an entry like hello@hello is seen as ok.

Thanks for your help.
0
Comment
Question by:joel011197
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
/\@+\.*/ matches one or more '@'s followd by 0 or more '.'s
so 'hello@hello' is ok, as is 'hello@@...hello'
/\@+.*\./ would fail on 'hello@hello' but match on 'hello@hello.'
But no regular expression can really check the validity of Email addresses, since RFC 822 is so complicated,
and regular expressions can't deal with arbitrarily nested constructs.
(but if you're willing to limit your email addresses to only one level of nesting in comments, then there is a 6598 byte long regular expression that comes close to being able to validate Email address)
0
 
LVL 1

Accepted Solution

by:
ptruman earned 50 total points
Comment Utility
Short of doing lots of work resolving the address and VRFY'ing
the address, you can do :

$NumDotsRequired = 1;
if ( $FORM{EMAIL} =~ /\@/ ) {
    $DotCount = $FORM{EMAIL}=~tr/././;
    if ( $DotCount >= $NumDotsRequired ) {
       $AddressValid = Yes;
    } else {
       $AddressValid = No;
    }
}

Some e-mail addresses only need 1 '.' char, ie.
someone@microsoft.com
So, decide how many you need to check for, and the above
code will check for the @ and the relevant number of '.' chars.

HTH



0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> But no regular expression can really check the validity of Email addresses, since RFC 822 is so complicated,

have seen an example for this in Jeffrey Friedl's "Mastering Regular Expressions"

(sorry have this book not handy)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
> have seen an example for this in Jeffrey Friedl's "Mastering Regular Expressions"
that example only handles one level of nesting in comments.
(which may be good enough for most purposes)
the example is available at
http://enterprise.ic.gc.ca/~jfriedl/regex/code.html

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
Thanks for the URL, ozo. I just had the ones from the book, which seem to be outdated.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now