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

Improve flexibility of email validation in the ValidationHelper #7310

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace Volo.Abp.Validation
{
public class ValidationHelper
{
private const string EmailRegEx = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
// Taken from W3C as an alternative to the RFC5322 specification: https://html.spec.whatwg.org/#valid-e-mail-address
// The RFC5322 regex can be found here: https://emailregex.com/
public static string EmailRegEx { get; set; } = @"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";

public static bool IsValidEmailAddress(string email)
{
Expand All @@ -13,7 +15,6 @@ public static bool IsValidEmailAddress(string email)
return false;
}

/*RFC 2822 (simplified)*/
return Regex.IsMatch(email, EmailRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,19 @@ public void Should_Validate_Emails()
{
//Valid
ValidationHelper.IsValidEmailAddress("john.doe@domain.com").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("john-doe1@domain.co").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("ip@1.2.3.123").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("pharaoh@egyptian.museum").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("john.doe+regexbuddy@gmail.com").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("Mike.O'Dell@ireland.com").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("admin@localhost").ShouldBe(true);
ValidationHelper.IsValidEmailAddress("j@h.c").ShouldBe(true);

//Invalid
ValidationHelper.IsValidEmailAddress("1024x768@60Hz").ShouldBe(false);
ValidationHelper.IsValidEmailAddress("not.a.valid.email").ShouldBe(false);
ValidationHelper.IsValidEmailAddress("john@aol...com").ShouldBe(false);
ValidationHelper.IsValidEmailAddress("john@aol@domain.com").ShouldBe(false);
ValidationHelper.IsValidEmailAddress("jack@domain.").ShouldBe(false);
}

[DependsOn(typeof(AbpAutofacModule))]
Expand Down