DEV Community

Cover image for Validate mail address format the easy way
Zohar Peled
Zohar Peled

Posted on

6 4

Validate mail address format the easy way

(First published on What the # do I know?)

We’ve seen it numerous times: Using a regular expression to validate a mail address.
Heck, you’re probably doing it yourself.

You might also recall that as a beginner, you wrote a method that use a couple of IndexOf methods on a string to make sure it has the at sign (@) and a dot (.) after it.

Well, with the .Net Framework, that long, unreadable, unmaintainable regular expression can be (and should be) thrown away, never to be used again.

So, how do you validate the format of an email address without a regular expression or looping over your input string? Simple – you use the tools the framework gives you and you try to create an instance of the MailAddress class from it.

One of the constructors of this class takes in a single string that is the address to use. This constructor will throw a FormatException if the string is not a valid mail address format – so all you have to do in order to validate an email address is this:

public bool IsEmailAddressWellFormatted(string address)
{
    try
    {
        var address = new MailAddress(address);
        return true;
    }
    catch (FormatException ex)
    {
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode

And that's it!
Now you have a mail address format validation that's readable, maintainable, and can easily be transformed to an extension method to string, if you want.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (3)

Collapse
 
jeikabu profile image
jeikabu

Looks like there will soon be an alternative without the exception (similar to Uri.TryCreate).

Collapse
 
peledzohar profile image
Zohar Peled

Good to know!

Collapse
 
michaeljota profile image
Michael De Abreu

Thanks! That's a bit of a helpful way to find out if that's a valid email. I wonder if there is a way to do it in other languages.

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay