Solved

email validation - match function

Posted on 2004-04-22
23
1,575 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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
Z

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

Expert Comment

by:jaysolomon
Comment Utility
sorry that was for gwyn
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
It allows: --@-.aa

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
And _______@somedomain.co.uk is a valid email address.

0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
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
Comment Utility
--@-.com is also valid. No one has as yet has developed a perfect email regex.
0
 
LVL 16

Expert Comment

by:jaysolomon
Comment Utility
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
Comment Utility
Mine may not be perfect but i beleive is close
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
I mean: mostly :)
0
 
LVL 16

Expert Comment

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

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
_______@_____.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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
In Domino you can even have multiple @ in a destination address to define the adjecant mail domains.
0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
'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
Comment Utility
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
Comment Utility
from what i get a / defines a group

i may be wrong
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

762 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