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

Regular expression question

P: n/a
/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/

I'm using this to try to validate a small subset of the valid e-mail
addresses allowed by the relevant RFC (alphanumerics, underscores, and
dashes). I've tested it and it seems to work - I'm just looking for
helpful hints :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/

I'm using this to try to validate a small subset of the valid e-mail
addresses allowed by the relevant RFC (alphanumerics, underscores, and
dashes). I've tested it and it seems to work - I'm just looking for
helpful hints :)


Well, one hint would be to not take such a restrictive subset. :)

The range [a-zA-Z0-9_] can be written as \w, and [.] and \. are
equivalent, so a shorter version is:

/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/

(I changed the last "*" to "+", because all domain names must have at
least one ".").

Again, I can't see what real purpose such a restrictive subset can
be used for. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

P: n/a
Christopher Benson-Manica wrote:
/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/


Rather than the character class

[a-zA-Z0-9_-]

you can use

[\w-]

I'd also escape the dots with a backslash, rather than specifying a
character class.

Making the last group optional is debatable. Whilst I believe it's
valid (an address could be a single string if it aliases another
destination), it's not of much value on the Web as all domains will
have at least one dot.

Out of interest, did you intend to make the two character classes
after the @ different, or is one of them a typo?

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

P: n/a
Michael Winter <m.******@blueyonder.co.invalid> spoke thus:
[\w-]
I wasn't aware you could do that - I will. Thanks.
Out of interest, did you intend to make the two character classes
after the @ different, or is one of them a typo?


It was a typo, which I suppose further argues in favor of the
abbreviated version you presented above :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #4

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> spoke thus:
Again, I can't see what real purpose such a restrictive subset can
be used for. :)


Do any real e-mail addresses use the other allowable characters? We
just wanted something fairly simple without characters that might
cause problems elsewhere.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #5

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Do any real e-mail addresses use the other allowable characters? We
just wanted something fairly simple without characters that might
cause problems elsewhere.


I regularly *try* to enter something on the form
na************@example.com
That allows me to quickly create a unique address for each recipient,
so I can see who gives my addres to spammers (and allows me to block it),
all using only one mailbox.

However "cause problems elsewhere" is so vague, that perhaps you
should decide what programs the address must interact with, and what
their requirements are. After all, you probably don't want to turn
somebody away, just because their e-mail address is not matching
the lowest common denominator, unless you *really* need to.

(but no, I don't usually see such addresses)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> spoke thus:
I regularly *try* to enter something on the form
na************@example.com
That allows me to quickly create a unique address for each recipient,
so I can see who gives my addres to spammers (and allows me to block it),
all using only one mailbox.
Hm, I see. I'm fairly certain that we're not a spam entity though :)
However "cause problems elsewhere" is so vague, that perhaps you
should decide what programs the address must interact with, and what
their requirements are. After all, you probably don't want to turn
somebody away, just because their e-mail address is not matching
the lowest common denominator, unless you *really* need to.


Well, I suppose if we get complaints, we can URIEncode the form fields
and allow some more of the characters, but that doesn't sound too
likely.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.