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
Solved

Regex Javascript expression help

Posted on 2010-09-23
11
364 Views
Last Modified: 2012-05-10
I have a javascript function that validates email addresses (see attached).  The problem is it returns FALSE for an email address with a + (plus) in it, e.g. "+amerFandB@ihg.com".  Can someone give me the proper syntax to allow it to accept a + as a valid character?
//Returns TRUE if the passed string appears to be a valid e-mail address 
function isEmailAddressValid (passedEmail) { 
var a = (!(String(passedEmail)).search(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.biz)|(\.gov)|(\.org)|(\.int)|(\.info)|(\.name)|(\.coop)|(\..{2,2}))$)\b/gi));        //general validity 
var b = (String(passedEmail).indexOf('@') == String(passedEmail).lastIndexOf('@'));                //only one @ 
var c = (String(passedEmail) == fullTrimString(passedEmail));                        //no whitespace 
return(a && b && c); 
}

Open in new window

0
Comment
Question by:Paulconsulting
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33743578
try changing line number 3 to

var a = (!(String(passedEmail)).search(/\b(^(\+){0,1}(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.biz)|(\.gov)|(\.org)|(\.int)|(\.info)|(\.name)|(\.coop)|(\..{2,2}))$)\b/gi));        


0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33743586
do you want '+' to be allowed only at the beginning only? or else where also?
0
 

Author Comment

by:Paulconsulting
ID: 33743670
begining only
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Paulconsulting
ID: 33743673
well rather before the @
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33743702
make it

var a = (!(String(passedEmail)).search(/\b(^([\S\+]+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.biz)|(\.gov)|(\.org)|(\.int)|(\.info)|(\.name)|(\.coop)|(\..{2,2}))$)\b/gi));        
0
 

Author Comment

by:Paulconsulting
ID: 33743780
its almost there...its letting me put in a + after the @ sign that should not be allowed
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33743805
can you share the test data with me? thanks
0
 

Author Comment

by:Paulconsulting
ID: 33743839
Should be allowed:
fdsa+@fdsa.com
fds+a@fdsa.com
+fdsa@fdsa.com
etc.  any + before the @ is ok

Should not be allowed:
fdsa@+fdsa.com
fdsa@fdsa+.com
fdsa@fdsa.+com
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33744076
this one worked for me
<html>
<body>

<script type="text/javascript">

function isEmailAddressValid (passedEmail) { 
var a = (!(String(passedEmail)).search(/(^(\S+@)[a-zA-Z0-9]+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.biz)|(\.gov)|(\.org)|(\.int)|(\.info)|(\.name)|(\.coop)|(\..{2,2}))$)/gi));     
return a ; 
}
document.writeln(isEmailAddressValid("fdsa+@fdsa.com")); 
document.writeln(isEmailAddressValid("fds+a@fdsa.com")); 
document.writeln(isEmailAddressValid("+fdsa@fdsa.com")); 
document.writeln(isEmailAddressValid("fdsa@+fdsa.com")); 
</script>

</body>
</html>

Open in new window

0
 

Author Closing Comment

by:Paulconsulting
ID: 33744126
Thanks for your help!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33744146
thanks for the points :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

839 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