Email Validator

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Email Validator

Post 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 ;)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Email Validator

Post 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?
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: Email Validator

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Email Validator

Post 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
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: Email Validator

Post by take2hikes »

Thanks ;-)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Email Validator

Post 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!
Last edited by prometheuzz on Mon Mar 16, 2009 1:17 pm, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Email Validator

Post by prometheuzz »

removed duplicate post
take2hikes
Forum Newbie
Posts: 14
Joined: Sat Feb 14, 2009 1:45 pm

Re: Email Validator

Post 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 :-/
Post Reply