Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex Javascript expression help

Posted on 2010-09-23
11
Medium Priority
?
379 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: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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

688 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