Solved

How to check an Email address?

Posted on 1998-10-06
5
130 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

825 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