By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,833 Members | 2,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,833 IT Pros & Developers. It's quick & easy.

Validate EMail with RegExp...

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a

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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.