[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to check an Email address?

Posted on 1998-10-06
5
Medium Priority
?
138 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 100 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

656 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