Web Design that taps into the haromny and vision of your dreams.

Best Way to Validate Email Addresses in C#

Better than RegEx? Use this tip for email validation in C#

Written By on in C#

113 words, estimated reading time 1 minutes.

It used to be that a RegEx was the best method for validating email addresses in C#, but with all the new Top Level Domains (TLD's) flying around and new ones being released, this is becoming an increasingly difficult task.

So instead, why not let Microsoft do the hard work of validating email addresses in C#? Their .Net library gets updated regularly with all the new validation methods automatically across all platforms (forms, asp.net etc..) meaning you need only call their methods from your code and you are up to date.

Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.

Source: Microsoft ASP.Net Website

public bool IsEmailValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

Last updated on: Tuesday 20th June 2017

 

 

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.

By submitting a message you agree to abide by our Terms and Comment Guidelines. If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me