Skip to content

Commit

Permalink
Fixed email validation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay committed Jul 15, 2013
1 parent 1356dc3 commit e83c9e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 13 additions & 5 deletions Facts/EmailValidationRegex.cs
Expand Up @@ -7,20 +7,24 @@ namespace NuGetGallery
/// <summary>
/// Notes, we do not follow strictly the RFCs at this time, and we choose not to support many obscure email address variants,
/// such as those with quotes and parentheses.
/// We may add internationalization or single-letter domain support in the future..?
/// We may add international character support in the function.
/// </summary>
public class EmailValidationRegex
{
[Fact]
public void TheWholeWorks()
[Theory]
[InlineData("fred@fred.com")]
[InlineData("a@b.co")]
[InlineData("a@b.c.d.e.f")]
public void TheWholeAllows(string address)
{
var match = new Regex(RegisterRequest.EmailValidationRegex).IsMatch("fred@fred.com");
var match = new Regex(RegisterRequest.EmailValidationRegex).IsMatch(address);
Assert.True(match);
}

[Theory]
[InlineData("fred@@fred.com")]
[InlineData("fred@fred@fred.com")]
[InlineData("fred@.com")]
public void TheWholeDoesntAllow(string testWhole)
{
var match = new Regex(RegisterRequest.EmailValidationRegex).IsMatch(testWhole);
Expand All @@ -45,6 +49,7 @@ public void TheFirstPartMatches(string testFirstPart)
[InlineData("fr@ed")]
[InlineData("fr\\ed")]
[InlineData("fr\"ed")]
[InlineData("fr()ed")]
[InlineData("fr[]ed")]
[InlineData("abc\"defghi\"xyz")] // thanks Wikipedia
[InlineData("abc.\"defghi\".xyz")] // thanks Wikipedia, but in practice nobody uses these email addresses.
Expand All @@ -59,14 +64,17 @@ public void TheFirstPartDoesntAllow(string testFirstPart)
[InlineData("XYZ.com")]
[InlineData("xyz.govt.nz")]
[InlineData("X1-Y2-Z3.net")]
[InlineData("b.co")]
[InlineData("b.co.uk")]
[InlineData("a.b.c.d.e.f")]
public void TheSecondPartMatches(string testSecondPart)
{
var match = new Regex("^" + RegisterRequest.SecondPart + "$").IsMatch(testSecondPart);
Assert.True(match);
}

[Theory]
[InlineData("i.com")] // single letter domains do exist, but we have never supported them yet...
[InlineData(".com")] //no top level domains
[InlineData("com")] //no top level domains
[InlineData("mailserver1")] //no hostname without top level domain
[InlineData("[1.1.1.1]")] //no IP addresses
Expand Down
9 changes: 7 additions & 2 deletions Website/RequestModels/RegisterRequest.cs
Expand Up @@ -6,9 +6,14 @@ namespace NuGetGallery
public class RegisterRequest
{
// Note: regexes must be tested to work in javascript
// (?<!\\.)
// We do NOT follow strictly the RFCs at this time, and we choose not to support many obscure email address variants.
// Specifically the following are not supported by-design:
// * Addresses containing () or []
// * Second parts with no dots (i.e. foo@localhost or foo@com)
// * Addresses with quoted (" or ') first parts
// * Addresses with IP Address second parts (foo@[127.0.0.1])
internal const string FirstPart = @"[-A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.]+";
internal const string SecondPart = @"[A-Za-z0-9][\w\.-]*[A-Za-z0-9]\.[A-Za-z][A-Za-z\.]*[A-Za-z]";
internal const string SecondPart = @"[A-Za-z0-9]+[\w\.-]*[A-Za-z0-9]*\.[A-Za-z0-9][A-Za-z\.]*[A-Za-z]";
internal const string EmailValidationRegex ="^" + FirstPart + "@" + SecondPart + "$";

internal const string EmailValidationErrorMessage = "This doesn't appear to be a valid email address.";
Expand Down

0 comments on commit e83c9e3

Please sign in to comment.