DEV Community

Praneet Nadkar
Praneet Nadkar

Posted on

3 2

Using EmailValidation in C#

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();
}
Enter fullscreen mode Exit fullscreen mode

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay