Solved

Regex Javascript expression help

Posted on 2010-09-23
11
352 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code help needed for an 'animated balance scale' 8 44
asp.net c# execute .bat server side problem 6 64
how to filter email.ro:pass? 5 36
Regex to filter only IP's? 4 43
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now