473,770 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate EMail with RegExp...

My understanding of regular expressions is rudimentary,
at best.

I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

TIA...

--
Dag.

Work is the curse of the drinking classes
-- Oscar Wilde
Nov 3 '05 #1
12 5025

Dag Sunde wrote:
My understanding of regular expressions is rudimentary,
at best.

I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

TIA...

--
Dag.

Work is the curse of the drinking classes
-- Oscar Wilde

Hi. A quick search on google threw up a million (well perhaps a
billion) examples of e-mail validation reg-exps.

Here is one I found to tinker with (worked when I tested it):-

var
r=/^([a-zA-Z][\w\.-]*[a-zA-Z0-9])@([a-zA-Z0-9][\w-]*[a-zA-Z0-9])\.([a-zA-Z][a-zA-Z\.]*[a-zA-Z])$/;

I suspect it has limits, such as, needs at least 2 characters in each
part of the e-mail address.

Julian

Nov 3 '05 #2
Dag Sunde wrote:
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.

Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?

Here's what got:

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


/\w/ (word character) is equivalent to /[A-Za-z0-9_]/ which does not
include the hyphen.

function validateEmail(e Mail)
{
return /^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

See also:
<http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Global_Objects: RegExp>
<http://msdn.microsoft. com/library/en-us/script56/html/js56jsobjregexp .asp>
and <http://www.mozilla.org/js/language/E262-3.pdf>, section 15.10.

To understand Regular Expressions better, you should
definitely read <http://www.oreilly.com/catalog/regex/> pp.
At least the example chapters of "Understand ing Regular
Expressions" have proven very useful to me in that regard.

Note that client-side scripting may not be supported,
so you need server-side validation as well.
PointedEars
Nov 3 '05 #3
"Thomas 'PointedEars' Lahn" <Po*********@we b.de> wrote in message
news:11******** ********@Pointe dEars.de...
Dag Sunde wrote:
<snipped/>

function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


/\w/ (word character) is equivalent to /[A-Za-z0-9_]/ which does not
include the hyphen.

function validateEmail(e Mail)
{
return /^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


Thank you!

See also:
<http://developer.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Global_Objects: RegExp>
<http://msdn.microsoft. com/library/en-us/script56/html/js56jsobjregexp .asp>
and <http://www.mozilla.org/js/language/E262-3.pdf>, section 15.10.
Brilliant! I really need to read that.
To understand Regular Expressions better, you should
definitely read <http://www.oreilly.com/catalog/regex/> pp.
At least the example chapters of "Understand ing Regular
Expressions" have proven very useful to me in that regard.
It is one of my pet shames that I never get around to read up on
Regular expressions... :-(

Note that client-side scripting may not be supported,
so you need server-side validation as well.


Here luckily, I'm God, and decides what must be supported.
It is an intranet site, and I've been given full freedom concerning
requirements like that.

--
Dag.
Nov 3 '05 #4
"Julian Turner" <ju****@baconbu tty.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .

Dag Sunde wrote:
<snipped/>
Hi. A quick search on google threw up a million (well perhaps a
billion) examples of e-mail validation reg-exps.

Typical... So didt I... but only after asking here. (Like a
bloody newbie) :-\
Here is one I found to tinker with (worked when I tested it):-

var
r=/^([a-zA-Z][\w\.-]*[a-zA-Z0-9])@([a-zA-Z0-9][\w-]*[a-zA-Z0-9])\.([a-zA-Z][a-zA-Z\.]*[a-zA-Z])$/;
I suspect it has limits, such as, needs at least 2 characters in each
part of the e-mail address.


It is very similar to the one I found...

Thank you for the response, Julian.

--
Dag.
Nov 3 '05 #5
Julian Turner wrote:
Dag Sunde wrote:
function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

[...]


Hi. A quick search on google threw up a million (well perhaps a
billion) examples of e-mail validation reg-exps.

Here is one I found to tinker with (worked when I tested it):-

var
r=/^([a-zA-Z][\w\.-]*[a-zA-Z0-9])@([a-zA-Z0-9][\w-]*[a-zA-Z0-9])\
([a-zA-Z][a-zA-Z\.]*[a-zA-Z])$/;

I suspect it has limits, such as, needs at least 2 characters in each
part of the e-mail address.


If I'm not mistaken, a Regular Expression for e-mail addresses adhering
closely to "3.4.1. Addr-spec specification" in RFC2822 "Internet Message
Format" would be:

addr-spec
==> local-part "@" domain
==> dot-atom / quoted-string / obs-local-part "@" domain
==> [CFWS] dot-atom-text [CFWS] / quoted-string / obs-local-part "@" domain

(We ignore comments and folding white space.)

==> 1*atext *("." 1*atext) / quoted-string / obs-local-part "@" domain

(We ignore quoted strings and obsolete addressing.)

==> 1*atext *("." 1*atext) "@" domain
==> [!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*
"@" dot-atom / domain-literal / obs-domain

(We ignore obsolete addressing again; and I have yet to see an e-mail
address produced by the domain-literal production, so I ignore this as
well.)

==> [!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*
@
[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*

Which leaves us with the fairly simple :)

/^[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+(\
[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+)*@[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+(\
[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+)*/

(Watch for word wrap!)

Several characters in the above range have adjacent code points in
ASCII, so we can compact it a little bit.

/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*@[!-'*+=?^-`{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*/

However, since we are on the Internet, I don't think this will suffice.
For example, I don't know a top-level domain with only one character
(I know such a second-level domain, though) and on today's Internet,
the domain-part should be a FQDN (fully qualified domain name):

/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*@[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+\.[!-'*+=
{-~\/-9A-Z^-z-]{2,}/

It could be further refined, allowing only existing top-level domains
and possible second-level domains, allowing especially for IDN domain
names aso., but I think this will suffice for now.

Of course, my tests showed addresses with hyphens are recognized as
correct and no false positives to date (using rather strange examples
and sender addresses of posters here). I'll be happy if you provide
me with one that does not fit :)
HTH

PointedEars
Nov 3 '05 #6
Thomas 'PointedEars' Lahn wrote:
/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*@[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+\.[!-'*+=
{-~\/-9A-Z^-z-]{2,}/


Consequently, it should be

/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=?{-~\/-9A-Z^-z-]+)*
@[!-'*+=?{-~\/-9A-Z^-z-]+\.[!-'*+=?{-~\/-9A-Z^-z-]{2,}/

(Watch for word wrap, of course.)
PointedEars
Nov 3 '05 #7
Dag Sunde wrote:
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.


http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
--
David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Nov 3 '05 #8
"David Dorward" <do*****@yahoo. com> wrote in message
news:dk******** ***********@new s.demon.co.uk.. .
Dag Sunde wrote:
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.


http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html


You're kidding me, right?

That is the longest monster of a regular expression I've ever seen!

--
Dag.
Nov 3 '05 #9
David Dorward wrote:
Dag Sunde wrote:
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.


http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html


It is to be noted that RFC822 has already been obsoleted by RFC2822
in April 2001 (the document referred to is dated 13/04/2002) where
the Address Specification this Regular Expression is based on differs
in certain regards.

Not to mention that the above referred no longer qualifies as being
simple (maybe exhaustive, but still OBSOLETE) and that it validates
the Address Specification, including sender name, address comments
and folding whitespaces not usually found in e-mail addresses input
in Web form controls, not the up-to-date addr-spec specification for
the e-mail address much more often to be found there.
PointedEars
Nov 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1969
by: michaaal | last post by:
I realize this is not solely an ASP question, but I thought you guys might know the answer and I couldn't find anywhere else to post this. If you have suggestions for this I am more than happy to post somewhere else. How can I, using ASP code, validate whether or not an email address is a real one? For example, if I have the email address called roy@rogers.net how can I determine if it is real?
10
7693
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following not appear to work: var email_address = "Joe@Schmoe"; var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$"); var result = email_regex.exec (email_address); alert (" result = \"" + result + "\"\n" + " result = \"" + result + "\"\n" + "...
13
4808
by: Eddie | last post by:
I need to validate a text input field. I just want to say if user enters 93101 or 93102 or 93103 or 93105 or 93106 or 93107 or 93108 or 93109 or 93110 or 93111 or 93116 or 93117 or 93118 or 93120 or 93121 or 93130 or 93140 or 93150 or 93160 or 93190 or 93199 or 93199 or 93401 or 93402 or 93403 or 93405 or 93406 or 93407 or 93408 or 93409 or 93410 or 93412
3
3380
by: MJ | last post by:
For some reason the following script does not work in Netscape/Mozilla, but works fine in IE and Opera. It is supposed to check the syntax, make sure there is a valid TLD (yes, those are all of the current TLDs), and allow for addresses with or without trailing slashes or page addresses. Anybody have any ideas on how to get this to work in Netscape? I suspect it has something to do with the regular expression, but I can't get it to...
24
7131
by: Arno R | last post by:
Hi all, I have a client with several shoe-shops. Customers can leave their email-address if they want to be notified when there is a sale. Input is validated with instr() I am checking for @ and . (required) and also checking for spaces (not allowed). But: A LOT (5-10%) of the addresses still are wrong; (provider doesn't exist) or email-address not valid (anymore). When sending bulk-mail its a nasty problem to get the false addresses...
3
2025
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some electronic document and paste into form field which it will probably mess up the program. for example, we have a customer who wants to enter AH12345 into one of our fields, it appears АН12345 in hidden field of our asp page, but it displayed...
23
2879
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which is not valid. Here's my attempt, neither of my regexps work quite how I want: import os import re
11
8195
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function to validate 3 types of fields: text, radio button, dropdown menu. but the code doesn't validate. After 2 days, it's time I asked for guidence. Criteria: Text: only alphabet, no numerals, allowed Radio: one must be selected, Dropdown: an option...
3
7232
by: samuelberthelot | last post by:
Hello, I would like to validate a date in a textbox on the onChange event. The date must be in the format 01/01/2007 I would like to use a regular expression to validate it but I'm not very familiar with them. How can I do it ? Thanks
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.