Using EmailValidation in C
The other day I was working on the email validations, and I came across this beautifully created nuget package called EmailValidation.
So here’s the link to it.
Now, the MSDN blog page has a short list of a few email addresses that can be used to validate this.
I just copied those and checked. This package works great!
So in a case where someone is looking out for this, try this.
static void Main()
{
var listOfValidEmails = new List<string>
{
“email@domain.com”,
“firstname.lastname@domain.com”,
“email@subdomain.domain.com”,
“firstname+lastname@domain.com”,
“email@123.123.123.123”,
“email@[123.123.123.123]”,
“\”email\”@domain.com”,
“1234567890@domain.com”,
“email@domain-one.com”,
“_______@domain.com”,
“email@domain.name”,
“email@domain.co.jp”,
“firstname-lastname@domain.com”
};
var listOfInvalidEmails = new List<string>
{
“plainaddress”,
“#@%^%#$@#$@#.com”,
“@domain.com”,
“Joe Smith <email@domain.com>”,
“email.domain.com”,
“email@domain@domain.com”,
“.email@domain.com”,
“email.@domain.com”,
“email..email@domain.com”,
“あいうえお@domain.com”,
“email@domain.com (Joe Smith)”,
“email@domain”,
“email@-domain.com”,
“email@domain.web”,
“email@111.222.333.44444”,
“email@domain..com”,
};
foreach (var email in listOfValidEmails)
{
Console.WriteLine($”{email} is {(EmailValidator.Validate(email) ? “valid” : “invalid”)}!”);
}
Console.WriteLine(“==============================================================================”);
foreach (var email in listOfInvalidEmails)
{
Console.WriteLine($”{email} is {(EmailValidator.Validate(email) ? “valid” : “invalid”)}!”);
}
Console.ReadKey();
}
This is quicker to use like just import a nuget and you are good to go. In my opinion this covers most of the scenarios for email validation.
One interesting thing I wanted to try is using a regex for all these and then try using pattern matching to check two things. One that all the email scenarios are covered and two, which approach is faster.
Top comments (0)