PDA

View Full Version : Documented Email Regex Validation



Jeff Wheeler
July 29th, 2006, 03:12 AM
Alright, inspired by the other thread, I wrote a regular expression in a Python function which will return a boolean whether or not the email is valid.

I followed the rfc (http://www.faqs.org/rfcs/rfc822.html) word-for-word. It is, as far as I know, exactly the same, except for support of ASCII characters 0x000-0x037 and 0x177 (those are special characters such as EOF, and backspace, which should be taken care of outside the script).


def match_email(address):
import re
mail_re = re.compile(r'(?#addr-spec)((?#local-part)((^|\.)((?#word)((?#quoted-string)"((?#quoted-pair)\\.|(?#qtext)[^"\\])+")|((?#atom)[^()<>@,;:\\".\[\] ]+)))+)@((?#domain)(?<=@|\.)((?#sub-domain)((?#domain-ref)((?#atom)[^()<>@,;: \\".\[\]]+))|((?#domain-literal)\[((?#dtext)[^\[\]\\]|(?#quoted-pair)\\.)+\])+)+($|\.)){2,}$')
return mail_re.match(address) != None

# Use like this:
print match_email(r'jeff@nokrev.com')

Some sample valid email addresses (believe it or not… just look at the rfc linked above):


"@ad \f".he."s\"".llo."@"@site.c[\]]m
"\"".hey." "@domain.doma["i"]n.domain
"\""."more".hey."@ @"@doma["i"]n.co.uk!

And some invalid ones:


"@ad \f"he."s\"".llo."@"@site.c[\]m
""".hey.""@domain.doma[]n.domain
@.com

Sorry for the bad samples… hehe.

Vexir
July 29th, 2006, 03:18 AM
Only god knows wth that is.. Good work? haha

Templarian
July 29th, 2006, 03:21 AM
^Couldn't of been said better. Nice Job.

... be normal check for '@' and a '.' or be a norkrev.

Pixelangry
July 29th, 2006, 03:22 AM
Im lost, :).

Goodjob? You seem awful excited on aim so it must be something good lol.

Jeff Wheeler
July 29th, 2006, 03:22 AM
hehe, thanks :love:

ramie
July 29th, 2006, 06:46 AM
Nice work man, I might put that through it's paces later if I get 20 mins.

You should take this code to the next level and provide some test cases for it and prove it works, Django has some nice unit testing baked in that would help.

Jeff Wheeler
July 29th, 2006, 11:36 AM
Actually, I think Django just uses PyUnit, a really popular unit testing thing for Python. Yeah, I was gonna do that, but I finished that at like 2:00AM… I'll do that today. :)

ramie
July 29th, 2006, 11:52 AM
I love TDD, it's defo the way forward for web apps these days, and actually saves loads of times, even tho it would appear to be more work.

I'm gonna port this to code ruby and have a play about.

Jeff Wheeler
July 29th, 2006, 12:08 PM
We all know Python pwns Ruby. ;)

ramie
July 29th, 2006, 12:10 PM
hahaha, dont get me started man :)

hl
July 29th, 2006, 12:15 PM
In order to help the lazy Nokrev. I shall explain to those of you who think I'm god.

Basically, it checks to see whether an email is valid or not. I personally prefer doing an (.*)@(.*).(.*) because I don't give a ****. However, Nokrev decided to try to interpret the RFC which states what is and what is not a valid email and make the PERFECT regex which will allow emails such as "@ad \f".he."s\"".llo."@"@site.c[\]]m which as we know.. no one is going to use... though it is valid. Therefore it should return true. Nokrev is basically anal about his functionality. Bleh.

ramie
July 29th, 2006, 12:32 PM
However he is correct and will be a much better programmer for it, it's not much more effort to do things properly.

How about goverment or company emails, like some.body@gov.ac.uk, email spoofing and jacking, sql injection etc, validation is a very important part of programming, it's better that you do it correctly!

Jeff Wheeler
July 29th, 2006, 03:11 PM
:lol:

Hehe, thanks. :)

hl
July 29th, 2006, 03:34 PM
Nokrev says I got pwnd. (.*)@(.*).(.*) will handle all that crap. Basically if it has an @ sign and a dot in it, I'm good...

Jeff Wheeler
July 29th, 2006, 03:35 PM
What if they input "@test.com. ;)

bootiteq
July 31st, 2006, 07:06 AM
then at least you have some amusing reading

Jeff Wheeler
July 31st, 2006, 12:01 PM
What?

bootiteq
August 2nd, 2006, 05:25 AM
Up until now I've been AS validating for a useless contact database which allows users to sms and email all their contacts.
I say useless cause it spits tacks if the info isnt formated perfectly. (we didnt design it)

With other contact db products like the one you can find here http://www.vision6.com.au/ It would seem this is no hasssle. Its got features which allows you to handle all the bounced mail. You can delete all dud contacts with a click.

I just feel checking for "." and "@" is fine now.
I think I could best spend my hours doing something more noticable, and as a result billable.

Cant believe I wasted so much time on something so trivial and unsellable.

:moustache

nice validation though