02/01/2015 by Nitesh

Validate Email Using Regex in C#


In this small post, I will show you how we can validate a string as proper email or not using Regex class in C#. Below is the code –

    public bool ValidateEmail(string emailAddress)
        string regexPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
        Match matches=Regex.Match(emailAddress,regexPattern);
        return matches.Success;

The above function takes a string parameter and returns if the string is a valid email address or not.

Hope this post helps you. Keep learning and sharing. Cheers.