Connecting Tech Pros Worldwide Help | Site Map

Validate EMail with RegExp...

Dag Sunde
Guest
 
Posts: n/a
#1: Nov 3 '05
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 dag-sunde@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


Julian Turner
Guest
 
Posts: n/a
#2: Nov 3 '05

re: Validate EMail with RegExp...



Dag Sunde wrote:
[color=blue]
> 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 dag-sunde@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[/color]


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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#3: Nov 3 '05

re: Validate EMail with RegExp...


Dag Sunde wrote:
[color=blue]
> 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 dag-sunde@test-domain.net too?
>
> Here's what got:
>
> function validateEmail(eMail) {
> return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
> }[/color]

/\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
Dag Sunde
Guest
 
Posts: n/a
#4: Nov 3 '05

re: Validate EMail with RegExp...


"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
news:1152332.1BXn9Yl26y@PointedEars.de...[color=blue]
> Dag Sunde wrote:
>[/color]
<snipped/>[color=blue][color=green]
>>
>> function validateEmail(eMail) {
>> return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
>> }[/color]
>
> /\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);
> }[/color]

Thank you!
[color=blue]
>
> 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.
>[/color]
Brilliant! I really need to read that.
[color=blue]
> 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.[/color]

It is one of my pet shames that I never get around to read up on
Regular expressions... :-(
[color=blue]
>
> Note that client-side scripting may not be supported,
> so you need server-side validation as well.[/color]

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.


Dag Sunde
Guest
 
Posts: n/a
#5: Nov 3 '05

re: Validate EMail with RegExp...


"Julian Turner" <julian@baconbutty.com> wrote in message
news:1131016264.556552.177680@z14g2000cwz.googlegr oups.com...[color=blue]
>
> Dag Sunde wrote:
>[/color]
<snipped/>[color=blue]
>
> Hi. A quick search on google threw up a million (well perhaps a
> billion) examples of e-mail validation reg-exps.
>[/color]

Typical... So didt I... but only after asking here. (Like a
bloody newbie) :-\
[color=blue]
> 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])$/;[/color]
[color=blue]
>
> I suspect it has limits, such as, needs at least 2 characters in each
> part of the e-mail address.[/color]

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

Thank you for the response, Julian.

--
Dag.


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Nov 3 '05

re: Validate EMail with RegExp...


Julian Turner wrote:
[color=blue]
> Dag Sunde wrote:[color=green]
>> function validateEmail(eMail) {
>> return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
>> }
>>
>> [...][/color]
>
> 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.[/color]

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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#7: Nov 3 '05

re: Validate EMail with RegExp...


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

Consequently, it should be

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

(Watch for word wrap, of course.)


PointedEars
David Dorward
Guest
 
Posts: n/a
#8: Nov 3 '05

re: Validate EMail with RegExp...


Dag Sunde wrote:
[color=blue]
> 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.[/color]

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
Dag Sunde
Guest
 
Posts: n/a
#9: Nov 3 '05

re: Validate EMail with RegExp...


"David Dorward" <dorward@yahoo.com> wrote in message
news:dkdlt7$f0k$1$830fa7b3@news.demon.co.uk...[color=blue]
> Dag Sunde wrote:
>[color=green]
>> 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.[/color]
>
> http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
>[/color]

You're kidding me, right?

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

--
Dag.


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#10: Nov 3 '05

re: Validate EMail with RegExp...


David Dorward wrote:
[color=blue]
> Dag Sunde wrote:[color=green]
>> 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.[/color]
>
> http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html[/color]

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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#11: Nov 3 '05

re: Validate EMail with RegExp...


Dag Sunde wrote:
[color=blue]
> "David Dorward" <dorward@yahoo.com> [...][color=green]
>> Dag Sunde wrote:[color=darkred]
>>> 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.[/color]
>> http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html[/color]
>
> You're kidding me, right?
>
> That is the longest monster of a regular expression I've ever seen![/color]

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
Dr John Stockton
Guest
 
Posts: n/a
#12: Nov 3 '05

re: Validate EMail with RegExp...


JRS: In article <lVkaf.7366$qE.1606245@juliett.dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang.javascript, Dag Sunde
<me@dagsunde.com> posted :
[color=blue]
>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 dag-sunde@test-domain.net too?
>
>Here's what got:
>
>function validateEmail(eMail) {
>return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
>}[/color]

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.
Dag Sunde
Guest
 
Posts: n/a
#13: Nov 4 '05

re: Validate EMail with RegExp...


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:DQ284TGYXoaDFwGa@merlyn.demon.co.uk...[color=blue]
> JRS: In article <lVkaf.7366$qE.1606245@juliett.dax.net>, dated Thu, 3
> Nov 2005 09:39:29, seen in news:comp.lang.javascript, Dag Sunde
> <me@dagsunde.com> posted :
>[color=green]
>>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 dag-sunde@test-domain.net too?
>>
>>Here's what got:
>>
>>function validateEmail(eMail) {
>>return /^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
>>}[/color]
>
> 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.[/color]

Of course.
[color=blue]
> 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>.[/color]

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.


Closed Thread


Similar JavaScript / Ajax / DHTML bytes