Solved

Regex Javascript expression help

Posted on 2010-09-23
11
366 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
[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
  • 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
Technology Partners: 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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

735 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