Solved

How to check an Email address?

Posted on 1998-10-06
5
133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205149
/\@+\.*/ 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
ID: 1205150
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
ID: 1205151
> 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
ID: 1205152
> 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
ID: 1205153
Thanks for the URL, ozo. I just had the ones from the book, which seem to be outdated.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

728 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