Improve company productivity with a Business Account.Sign Up

x
?
Solved

Regex Javascript expression help

Posted on 2010-09-23
11
Medium Priority
?
396 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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:
Gurvinder Pal Singh earned 2000 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:Gurvinder Pal Singh
ID: 33744146
thanks for the points :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

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.

Join & Write a Comment

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…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

585 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