Email RegExp

Ok so knightEknight and i were talking about email regs
below you will see what he came up with
We are trying to get this to catch 99.99% of errors.
There are tons and tons of patterns out there, but i have yet to see
one that will catch 99.99% of errors

So if anyone would like to contribute then that would be fine, but we do not want to have to
use 2 expressions hence isEmail(str) && isEmail2(str) (I think you know what i mean)

This is a continuation from http:Q_20899186.html
===================
<script type="text/javascript">
<!--
function isEmail(str){
    var objRegExp =  /^((?:(?:(?:(\w|~)[~\.\-\+]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
    return objRegExp.test(str);
}    
res1 = "me@here.commacommacom";
res2 = "me.you@here.c_m";
res3 = "me@-here.com";
res4 = "-me@here.com";
res5 = "me.you@here.com";
res6 = "me_here_there.everywhere@here_-jay.com";
res7 = "m_e.you@h__4.com";
res8 = "00000-@0-0000-0000.com";
res9 = "yabba-dabba~doo@scooby_doo.com";
res0 = "_~~~~~~~@_____.com";

document.write(res1 +" = "+ isEmail(res1) +"<br />");
document.write(res2 +" = "+ isEmail(res2) +"<br />");
document.write(res3 +" = "+ isEmail(res3) +"<br />");
document.write(res4 +" = "+ isEmail(res4) +"<br />");
document.write(res5 +" = "+ isEmail(res5) +"<br />");
document.write(res6 +" = "+ isEmail(res6) +"<br />");
document.write(res7 +" = "+ isEmail(res7) +"<br />");
document.write(res8 +" = "+ isEmail(res8) +"<br />");
document.write(res9 +" = "+ isEmail(res9) +"<br />");
document.write(res0 +" = "+ isEmail(res0) +"<br />");
// -->
</script>


jAy
LVL 16
jaysolomonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jaysolomonAuthor Commented:
BTW

Quote from the thread from kEk

>>>
Jay, although technically valid, I think I would like to prevent two identical special characters in a row, so none of this:

 x--x@here.com
 x__x@here.com
 x~~x@here.com
 x..x@here.com  // the expression already handles this well!

but this should be ok because the special chars are not identical:

  x~.x@here.com
<<<
0
ZvonkoSystems architectCommented:
Here my version:

<script type="text/javascript">
<!--
function isEmail(str){
    var objRegExp =  /^((?:(?:(?:(\w|~)[~\.\-\+]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
    return objRegExp.test(str);
}    
function isEmail2(str){
    var objRegExp =  /^[a-z][a-z0-9]+([\.\-\_\~][a-z]+)*\@([a-z][a-z0-9]+[\.\-\_\~])*[a-z][a-z0-9]+\.([a-z]{2,6})$/i;
    return objRegExp.test(str);
}    
res1 = "me@here.commacommacom";
res2 = "me.you@here.c_m";
res3 = "me@-here.com";
res4 = "-me@here.com";
res5 = "me.you@here.com";
res6 = "me_here_there.everywhere@here_-jay.com";
res7 = "m_e.you@h__4.com";
res8 = "00000-@0-0000-0000.com";
res9 = "yabba-dabba~doo@scooby_doo.com";
res0 = "_~~~~~~~@_____.com";

document.write(res1 +" = "+ isEmail(res1) +"<br />");
document.write(res2 +" = "+ isEmail(res2) +"<br />");
document.write(res3 +" = "+ isEmail(res3) +"<br />");
document.write(res4 +" = "+ isEmail(res4) +"<br />");
document.write(res5 +" = "+ isEmail(res5) +"<br />");
document.write(res6 +" = "+ isEmail(res6) +"<br />");
document.write(res7 +" = "+ isEmail(res7) +"<br />");
document.write(res8 +" = "+ isEmail(res8) +"<br />");
document.write(res9 +" = "+ isEmail(res9) +"<br />");
document.write(res0 +" = "+ isEmail(res0) +"<br />");
document.write("===============================<br>");
document.write(res1 +" = "+ isEmail2(res1) +"<br />");
document.write(res2 +" = "+ isEmail2(res2) +"<br />");
document.write(res3 +" = "+ isEmail2(res3) +"<br />");
document.write(res4 +" = "+ isEmail2(res4) +"<br />");
document.write(res5 +" = "+ isEmail2(res5) +"<br />");
document.write(res6 +" = "+ isEmail2(res6) +"<br />");
document.write(res7 +" = "+ isEmail2(res7) +"<br />");
document.write(res8 +" = "+ isEmail2(res8) +"<br />");
document.write(res9 +" = "+ isEmail2(res9) +"<br />");
document.write(res0 +" = "+ isEmail2(res0) +"<br />");
// -->
</script>



0
ZvonkoSystems architectCommented:
And by the way, I steal believe that one and only realy usefull RegExp for doing Email address checking is this:
http://javascript.internet.com/forms/email-validation---basic.html

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
  return (true)
}

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:
Of course are also ip addresses allowed and nonsese characters. But do you realy need such customers?
0
jaysolomonAuthor Commented:
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

is not a good one in my opinion as it allows


me.you@here.c_m = true
me.you@here.com = true // which is valid
me_here_there.everywhere@here_-jay.com = true // questionable
m_e.you@h__4.com = true // not likely but questionable
0
GwynforWebCommented:
The types of valid email addresses is near endless (LANS generate really odd ones) no one has come up with the perfect regex yet. The best one around is by Jeferrey Friedl (author of th O'Reilly book Regular Expressions) it is pages long and is generated by a perl program linked of his home page. (can not find the link just yet).To ensure all valid e-mails are passed it is inevitable that illegal ones will go through. All you can do is to try and minimise this. I use this

<script>
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}

alert(isValidEmail('Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us'))
</script>


example valid email addresses

~Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us
Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us
www@bull.bullnet.co.uk
frederick@stat.ucsa.edu
bob.mcalpine@mpr.gov.on.ca
tpjones/psw_rfl@fs.fed.us
17584c.gross/inra@123.03.201.12
*abc/cde@fgh.com
abc%def@123.1.1.1
abcdef@123.1.1.1
17584c.gross.ag.poster/inra@123.03.201.12
0
knightEknightCommented:
Here are some of my test addresses.  I would expect the first group to be valid and the second group to be invalid.

// expecting true;
""
"me@here.com"
"me+you@there.com"
"me-n-you@there.com"
"me1.you2@every.where.com"
"1me.2you3@no-where.com"
"~tilde1@here.com"
"tilde2~@here.com"
"tilde~3@here.com"
"x@y.zz"

// expecting false;
null
"me@here.c_m"
"me@here@com"
"me@.here.com"
"me.@here.com"
"me&you@here.com"
"me..you@here.com"
"me@here..com"
"me@here"
"here.com"
"@here.com"
"me.you@"
"me.you@here."
"me you@there.com"
"meNyou@the re.com"
"me@here.commacommacom"
"xx@yy.z"
0
knightEknightCommented:
sidebar ...

Hi Gwyn, I signed onto ICC late last night and registered my favorite handle "knightEknight" ... it was still available to I took it :)   I played terrible though -- it was after midnight.  What is your husbands handle?  Tell him to check out www.uschesslive.org because the interface is similar but it is free!
0
GwynforWebCommented:
The handle is something like  knightE_knight or KnightEKnight or some variation on that. I will pass the link on. Hubby is on EE ( a trouble maker :-) ) and his handle is not hard to guess if you know who he is. (I better ask him first before giving it out)
0
GwynforWebCommented:
he plays mindless bullet chess mostly . The family shares the account to some extent (unbeknowest to him) fortunately he  rarely notices when his rating goes down by a couple of 100.
0
jaysolomonAuthor Commented:
what you mean EE has trouble maker's?

no you do not say :)
0
knightEknightCommented:
my best guess would be Xxavier but perhaps it is Shekerra, no?
0
GwynforWebCommented:
:o)
0
knightEknightCommented:
sorry ... once the curiosity bug bit me I couldn't resist outing him.  :)
0
knightEknightCommented:
Here is an updated version of the first expression that will allow / in the first part of the address, which Gwyn pointed out is valid and even common in government circles.

var objRegExp =  /^((?:(?:(?:(\w|~)[~\.\-\+]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;

Also, I notice that this expression already does not allow two identical special characters in a row (except for _ and ~) which is what I wanted anyway.  I am happy with it as is unless someone can point out a major flaw in it.  Surely anyone who wants to take the time to intentionally create an invalid address that will pass this expression or a valid address that won't pass will be able to do so in time, but as it is this will catch nearly all the possible user typos.
0
knightEknightCommented:
oops!  that wasn't the updated version  :8)

/^((?:(?:(?:(\w|~)[~\.\-\+\/]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
knightEknightCommented:
and I'm working on a different approach:


var replied = false;

function isEmail(str)
{
   location = "mailto://" + str + "&BODY=Please reply to the email asap!";

   setInterval("if(checkForReply(str))replied=true",300000);

   if (replied)
      return true;
   else
      return false;
}
0
GwynforWebCommented:
that's hilarious :o)
0
ren_bCommented:
there has got to be a gajillion different ways you could do this :P

heres another more server sided approach, not really a regexp. dont know if people do this - check if its all real chars and what not, and then ping the domain and see if its real/up as a final validation.

#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
@email = split(/=/,$ENV{'QUERY_STRING'}); #?email=somewhere@myhome.com
@hostname = split(/@/,$email[1]);
$ping = `nmap -sP $hostname[1]`;
if($ping=~/appears to be up/s){
 ...continue with email...
} else {
 ...send back...
}

can use nmap or whatever utitility i suppose.
0
ren_bCommented:
here is one that matches the RFC822 standard: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
0
knightEknightCommented:
thanks ren!  that's great!  now if I can just figure out how to deal with the out-of-memory exception  ;)
0
GwynforWebCommented:
I wonder if it can be optimised. But wow what regex
0
jaysolomonAuthor Commented:
i agree

WOW!!!!!!!! what a regexp
0
KNVB HKSITOCommented:
May be try this one:
function checkemail(a)
{var testresults;
 var str=a;
 var filter=/^.+@.+\..{2,3}$/
 if (filter.test(str))
   testresults=true;
 else
  {
   alert("Please input a valid email address!");
   testresults=false;
  }
 return (testresults)
}
0
ren_bCommented:
me@localhost, mom@moms_g4.local, etc. :P perfectly valid, working emails at my home.
0
jaysolomonAuthor Commented:
kEk

Have you came to a conclusion on which you think we should go with?
0
jaysolomonAuthor Commented:
kEk

Have you came to a conclusion on which you think we should go with?
0
jaysolomonAuthor Commented:
sorry for dbl post EE oldlook error
0
knightEknightCommented:
They are all good Jay.  Split them with everyone (except me, ofcourse)
0
knightEknightCommented:
... but I'm going with the last one I posted.  It does what I need it to do and is short enough to maintain if necessary.
0
knightEknightCommented:
c'mon Jay, split these points up.  You already had my expression before you posted the question (save for a few minor updates:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.