Regular expression question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

/^[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 :)
 
L

Lasse Reichstein Nielsen

Christopher Benson-Manica said:
/^[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
 
M

Michael Winter

Christopher said:
/^[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
 
C

Christopher Benson-Manica

Michael Winter said:

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 :)
 
C

Christopher Benson-Manica

Lasse Reichstein Nielsen said:
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.
 
L

Lasse Reichstein Nielsen

Christopher Benson-Manica said:
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
(e-mail address removed)
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
 
C

Christopher Benson-Manica

Lasse Reichstein Nielsen said:
I regularly *try* to enter something on the form
(e-mail address removed)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top