Page 1 of 1

Email Validator

Posted: Mon Mar 16, 2009 11:17 am
by take2hikes
I was reading a book earlier and they had a regex that was.. well, flawed.

How does this look to you guys?

Code: Select all

^[a-zA-Z]+[a-zA-Z0-9_\-.]*@[a-zA-Z0-9]+[a-zA-Z0-9\-.]*\.com|net|info|tv|org|biz|au|eu$
I know there is a crazy regex out there that is the 'standard', but I think for most uses this one will work.

Let me know what you think ;)

Re: Email Validator

Posted: Mon Mar 16, 2009 11:38 am
by prometheuzz
take2hikes wrote:I was reading a book earlier and they had a regex that was.. well, flawed.

How does this look to you guys?

Code: Select all

^[a-zA-Z]+[a-zA-Z0-9_\-.]*@[a-zA-Z0-9]+[a-zA-Z0-9\-.]*\.com|net|info|tv|org|biz|au|eu$
I know there is a crazy regex out there that is the 'standard', but I think for most uses this one will work.

Let me know what you think ;)
I won't comment on the "correctness" of the regex, since I find validating e-mail addresses nonsense: if it is important enough for the user/client to receive e-mail, s/he will give a valid address. If it's solely for the purpose of eliminating typo's the user might make, just let him/her submit the address twice. And (trying) to validate email addresses may lead to valid addresses being rejected by your pattern: which is something a user may find very annoying.

That said, the | (the OR) in regex has a very low precedence, which means that your pattern is interpreted like this:

Code: Select all

^[a-zA-Z]+[a-zA-Z0-9_\-.]*@[a-zA-Z0-9]+[a-zA-Z0-9\-.]*\.com
OR
net
OR
info
OR
tv
OR
org
OR
biz
OR
au
OR
eu$
Well, just one comment on the "correctness" of your regex: what about ".co.uk" and "de" top level domain names?

Re: Email Validator

Posted: Mon Mar 16, 2009 11:52 am
by take2hikes
Nod, I could always add more domains. It's a toss up between adding more domains via the OR method or just making it certain length accepted.

Or, you could just allow it to accept anything after the domain. portion.

I understand your issue with validating email addresses. While the compare would make perfect sense, some people would want you to implement an email checker :-/

If you make your way back to this post, I could use some extra viewers over at my other post:
viewtopic.php?f=1&t=97016

Re: Email Validator

Posted: Mon Mar 16, 2009 12:11 pm
by prometheuzz
take2hikes wrote:Nod, I could always add more domains. It's a toss up between adding more domains via the OR method or just making it certain length accepted.

...
Just to be sure, you current regex:

Code: Select all

^[a-zA-Z]+[a-zA-Z0-9_\-.]*@[a-zA-Z0-9]+[a-zA-Z0-9\-.]*\.com|net|info|tv|org|biz|au|eu$
will also accept a string like "net" or "info" as a valid address because of the low precedence of the OR. You'll want to do something like:

Code: Select all

^[a-zA-Z]+[a-zA-Z0-9_\-.]*@[a-zA-Z0-9]+[a-zA-Z0-9\-.]*\.(com|net|info|tv|org|biz|au|eu)$
instead.

HTH

Re: Email Validator

Posted: Mon Mar 16, 2009 12:22 pm
by take2hikes
Thanks ;-)

Re: Email Validator

Posted: Mon Mar 16, 2009 1:16 pm
by prometheuzz
take2hikes wrote:Thanks ;-)
You're welcome.
Btw, I glanced at your other post, but I am not familiar with PHP design patterns/best practices: I only know a bit of regex.

Good luck with your blog!

Re: Email Validator

Posted: Mon Mar 16, 2009 1:16 pm
by prometheuzz
removed duplicate post

Re: Email Validator

Posted: Mon Mar 16, 2009 1:19 pm
by take2hikes
prometheuzz wrote:
take2hikes wrote:Thanks ;-)
You're welcome.
Btw, I glanced at your other post, but I am not familiar with PHP design patterns/best practices: I only know a bit of regex.

Good luck with your blog!

Well, I'll keep you in mind if the day comes where I need a super-regex ;-).

I wish you knew more about the OOP aspects of PHP... since you're the only one that seems to be strolling the forum :-/