Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsValidEmail() Improved Regex Pattern #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public static partial class Extensions
/// <returns>true if valid email, false if not.</returns>
public static bool IsValidEmail(this string obj)
{
return Regex.IsMatch(obj, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z0-9]{1,30})(\]?)$");
return Regex.IsMatch(obj, @"^\w+([-+.']\w+)*@(\[*\w+)([-.]\w+)*\.\w+([-.]\w+\])*$");
}
}
67 changes: 54 additions & 13 deletions test/Z.Core.Test/System.String/String.IsValidEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// License (MIT): https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
// More projects: https://zzzprojects.com/
// Copyright � ZZZ Projects Inc. All rights reserved.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Z.Core.Test
Expand All @@ -15,26 +14,68 @@ public class System_String_IsValidEmail
[TestMethod]
public void IsValidEmail()
{
// Valid Emails
{
// Type
string @this = "[email protected]";
// Arrange
var validEmails = new[]
{
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"email@[123.123.123.123]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
};

// Exemples
var result = @this.IsValidEmail(); // return true;
// Act
foreach (var email in validEmails)
{
// Act
var result = email.IsValidEmail();

// Unit Test
Assert.IsTrue(result);
// Assert
Assert.IsTrue(result);
}
}

// Invalid Emails
{
// Type
string @this = "[email protected]";
// Arrange
var invalidEmails = new[]
{
"plainaddress",
"#@%^%#$@#$@#.com",
"@example.com",
"Joe Smith <[email protected]>",
"email.example.com",
"email@[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected] (Joe Smith)",
"email@example",
"[email protected]",
//"[email protected]", // Should not be valid, not a top level domain, how should we handle this outside regex? Lookup dictionary of all valid top level domains? https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
"[email protected]",
"[email protected]"
};

// Exemples
var result = @this.IsValidEmail(); // return true;
// Act
foreach (var email in invalidEmails)
{
// Act
var result = email.IsValidEmail();

// Unit Test
Assert.IsTrue(result);
// Assert
Assert.IsFalse(result);
}
}
}
}
Expand Down