Solved

email validation - match function

Posted on 2004-04-22
23
1,576 Views
Last Modified: 2009-12-16
Hi,
I have this part of an email validation function which works fine:

var emailStr = "eyal@yahoo.com"
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var matchArray = emailStr.match(emailPat);
if (matchArray == null)
  return false;

I need to change the emailPat so it will accept email addresses of the pattern:
eyal.us@somedomain.co.uk
Hence accepting one or more dot (.)  characters before the @ character.

How do I do this?
Thank you.
0
Comment
Question by:eyal_barouk
  • 9
  • 8
  • 6
23 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10888733
<script type="text/javascript">
<!--
var isEmail = function(strValue){
      var objRegExp = /^[a-z][a-z0-9]*([.\-_][a-z][a-z0-9]*)*@([a-z][a-z0-9]*.)*([a-z]{2}|com|net|org|biz|gov|pro|int|mil|edu|info|name|aero|coop|museum)$/i;
    return objRegExp.test(strValue);
}
var output = "eyal.us@somedomain.co.uk"
alert(isEmail(output));
// -->
</script>
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
ID: 10888948
Or like this:

var emailStr = "eyal@yahoo.com"
var emailPat = /^(\".+\"|[a-z]\w*(\.[a-z]\w*)*)@(\[\d{1,3}(\.\d{1,3}){3}]|[a-z]\w*(\.[a-z]\w*)+)$/i;
var matchArray = emailStr.match(emailPat);
if (matchArray == null)
  return false;


0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10888971
Thje most comprehensive and practical e-mail regex I know is

<script>
function isEmail(email){
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
   
return re.test(email)
}

output = "eyal.us@somedomain.co.uk"
alert(isEmail(output));

</script>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889007
Z

and it allows
output = "_______@somedomain.co.uk"
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889021
sorry that was for gwyn
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889088
It allows: --@-.aa

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889107
And _______@somedomain.co.uk is a valid email address.

0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10889125
Jay yes it does because it is a valid email address,a strange one yes but _ is considered an alphanumeric character.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10889177
--@-.com is also valid. No one has as yet has developed a perfect email regex.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889186
No punt intended but

i will reject any dummy that trys to have a _______@_____.com email addy


There is just to many names in this world for that
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889195
Mine may not be perfect but i beleive is close
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.

 
LVL 63

Expert Comment

by:Zvonko
ID: 10889343
Myne is perfect :-)

<script>
function isEmail(theAddress){
  return !(!theAddress.match(/^\w+([.\-]\w+)*@\w+([.\-]\w+)+$/));
}

var emailStr = "eyal.be@yahoo.com"
alert(emailStr+" : "+isEmail(emailStr));
alert("123@456.ab : "+isEmail("123@456.ab"));
</script>



(...and most comprehensive and most practical :)
LOL :-)


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889351
I mean: mostly :)
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889377
LOL Zvonko and yours allows
alert("____@____.___"+isEmail("____@____.___"));
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889421
As we just now learned is the domain ____.___ allowed.
It consist of alphanumeric characters and TLD's will be soon (or are already) also allowed to be anything. So why check anyway?

0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10889605
_______@_____.com is not vaild _ is not allowed in the hostname.

Jay yours is no where near perfect it rejects many valid ones, eg

17584c.gross/inra@123.03.201.12
Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us
abc%def@123.1.1.1

There is discussion about this at http:Q_20488313.html . The best that is possible without hundreds of lines of codes is to allow through those that are valid together with a few invalid ones.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10889646
Zvonko, Yours rejects, (all of which are valid)

17584c.gross/inra@123.03.201.12
Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us
tpjones/psw_rfl@fs.fed.us
abc%def@123.1.1.1
*abc/cde@fgh.com
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10889672
gross/inra <-- Never had or seen an email with the slash

%def <-- Same here

i would assume anyone setting up emails addys would only use - _ .eg
john.doe
john.doe.doe

john-doe
john-doe-doe

john_doe
john_doe_doe

anyways i am the one validating my form and i will say what is allowed and if they have one of those goofy emails, then perhaps they are too goofy for me to work for or with
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889738
Slashes are not valid in internet email addresses.
They are valid in Lotus Notes internal email addresses for hierachilcal organization levels, but are converted by Domino before passed to SMTP.
The Domino admin does define the conversion rules for inbound and outbound conversion.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10889769
In Domino you can even have multiple @ in a destination address to define the adjecant mail domains.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10889876
'Slashes are not valid in internet email addresses.' yes they are.

Many US Government agency addresses have slashes in them. Dick_R_Gratton/SPM/USDAFS@notes.fs.fed.us is a real email address slightly changed.

0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10890082
well maybe one of you have more time than i so read this and the construct to it

http://www.faqs.org/rfcs/rfc2822.html
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10890123
from what i get a / defines a group

i may be wrong
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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 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

11 Experts available now in Live!

Get 1:1 Help Now