Solved

How to check an Email address?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 195
perl split by | 2 111
Perl DBI Transactions Using Custom Module 7 48
combine multiple lines 2 74
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

733 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