Return true when the E-mail address is valid - CSharp System.Net.Mail

CSharp examples for System.Net.Mail:SmtpClient

Description

Return true when the E-mail address is valid

Demo Code


using System.Text.RegularExpressions;

public class Main{
        #region IsEmailAddressValid//from w w  w  . j a  v  a2  s. com
        /// <summary>
        /// Return true when the E-mail address is valid
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        public static bool IsEmailAddressValid(string emailAddress)
        {
            if (string.IsNullOrEmpty(emailAddress))
                return false;

            var regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
            var matches = regex.Matches(emailAddress);

            return matches.Count == 1;
        }
}

Related Tutorials