Skip to content

Instantly share code, notes, and snippets.

@saarons
Created May 28, 2010 23:36
Show Gist options
  • Save saarons/417891 to your computer and use it in GitHub Desktop.
Save saarons/417891 to your computer and use it in GitHub Desktop.
# Shamelessly ripped from http://fightingforalostcause.net/misc/2006/compare-email-regex.php
describe "The perfect email regex" do
before(:all) do
@regex = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i
end
def matches(email)
!!email.match(@regex)
end
def self.it_should_match(email)
it "should be valid for #{email}" do
matches(email).should be_true
end
end
def self.it_should_not_match(email)
it "should be invalid for #{email}" do
matches(email).should be_false
end
end
# The following are valid emails
it_should_match("l3tt3rsAndNumb3rs@domain.com")
it_should_match("has-dash@domain.com")
it_should_match("hasApostrophe.o'leary@domain.org")
it_should_match("uncommonTLD@domain.museum")
it_should_match("uncommonTLD@domain.travel")
it_should_match("uncommonTLD@domain.mobi")
it_should_match("countryCodeTLD@domain.uk")
it_should_match("countryCodeTLD@domain.rw")
it_should_match("lettersInDomain@911.com")
it_should_match("underscore_inLocal@domain.net")
it_should_match("IPInsteadOfDomain@127.0.0.1")
it_should_match("IPAndPort@127.0.0.1:25")
it_should_match("subdomain@sub.domain.com")
it_should_match("local@dash-inDomain.com")
it_should_match("dot.inLocal@foo.com")
it_should_match("a@singleLetterLocal.org")
it_should_match("singleLetterDomain@x.org")
it_should_match("&*=?^+{}'~@validCharsInLocal.net")
it_should_match("foor@bar.newTLD")
# The following are invalid emails
it_should_not_match("missingDomain@.com")
it_should_not_match("@missingLocal.org")
it_should_not_match("missingatSign.net")
it_should_not_match("missingDot@com")
it_should_not_match("two@@signs.com")
it_should_not_match("colonButNoPort@127.0.0.1:")
it_should_not_match("")
it_should_not_match("someone-else@127.0.0.1.26")
it_should_not_match(".localStartsWithDot@domain.com")
it_should_not_match("localEndsWithDot.@domain.com")
it_should_not_match("two..consecutiveDots@domain.com")
it_should_not_match("domainStartsWithDash@-domain.com")
it_should_not_match("domainEndsWithDash@domain-.com")
it_should_not_match("numbersInTLD@domain.c0m")
it_should_not_match("missingTLD@domain.")
it_should_not_match("! \"()#$\%,/;<>[]`|@invalidCharsInLocal.org")
it_should_not_match("invalidCharsInDomain@! \"()#$\%,/;<>_[]`|.org")
it_should_not_match("local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment