473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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(eMail) {
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 4986

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(eMail) {
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(eMail) {
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(eMail)
{
return /^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}

See also:
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegEx p>
<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 "Understanding 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*********@web.de> wrote in message
news:11****************@PointedEars.de...
Dag Sunde wrote:
<snipped/>

function validateEmail(eMail) {
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(eMail)
{
return /^(\w+\.)*([\w-]+)@([\w-]+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


Thank you!

See also:
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegEx p>
<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 "Understanding 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****@baconbutty.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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(eMail) {
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*******************@news.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
Dag Sunde wrote:
"David Dorward" <do*****@yahoo.com> [...]
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!


And probably the most useless one (in this newsgroup), taking into account
that it is based on an obsolete RFC, is highly unlikely to be needed in
forms, and uses features no JS/ECMAScript engine supports.

Nice try, though.
PointedEars
Nov 3 '05 #11
JRS: In article <lV*******************@juliett.dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang.javascript, Dag Sunde
<me@dagsunde.com> posted :
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(eMail) {
return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}


It is pointless to attempt to validate an E-mail address format in
detail, unless you are writing an E-mail system, or unless the task is
purely pedagogical. A RegExp that will accept me@dagsunde.com will also
accept im@dogsendu.com, and for any given message at least one of those
is wrong. There's no way round it; someone typing an E-address just has
to be careful to get it right.

If a test does not accept any RFC-permitted E-address, there will be
complaints; if an allegedly-full test fails to reject any non-permitted
address, there will be complaints. And if the RFCs change the format
range, full-check code will have to be changed too.

Testing with /.+@.+\..+/ or /^.+@.+\..+$/, to ensure at least x@y.z,
does make sense as a check that something like an E-address has been put
in the field, rather than say a price or an arbitrary vulgarity.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm#VEmA>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 3 '05 #12
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:DQ**************@merlyn.demon.co.uk...
JRS: In article <lV*******************@juliett.dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang.javascript, Dag Sunde
<me@dagsunde.com> posted :
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(eMail) {
return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}
It is pointless to attempt to validate an E-mail address format in
detail, unless you are writing an E-mail system, or unless the task is
purely pedagogical. A RegExp that will accept me@dagsunde.com will also
accept im@dogsendu.com, and for any given message at least one of those
is wrong. There's no way round it; someone typing an E-address just has
to be careful to get it right.


Of course.
If a test does not accept any RFC-permitted E-address, there will be
complaints; if an allegedly-full test fails to reject any non-permitted
address, there will be complaints. And if the RFCs change the format
range, full-check code will have to be changed too.

Testing with /.+@.+\..+/ or /^.+@.+\..+$/, to ensure at least x@y.z,
does make sense as a check that something like an E-address has been put
in the field, rather than say a price or an arbitrary vulgarity.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm#VEmA>.


That is the only thing i want, to verify that the input at least can
be interpreted as an email-address. My problem was that my former regexp.
balked at 'x' or 'z' with hypens in them.

--
Dag.
Nov 4 '05 #13

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

Similar topics

10
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...
10
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...
13
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...
3
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...
24
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 @...
3
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...
23
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...
11
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...
3
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.