Checking if e-mail address is correct via Regex - CSharp System.Text.RegularExpressions

CSharp examples for System.Text.RegularExpressions:Match Email

Description

Checking if e-mail address is correct via Regex

Demo Code


using System.Text.RegularExpressions;

public class Main{
        /// <summary>
		/// Checking if e-mail address is correct
		/// </summary>
		/// <param name="eMail">E-mail address</param>
		/// <returns></returns>
		public static bool ValidateEMail(string eMail)
		{//from  w  ww . j  a  va 2 s.  c  o m
			if(string.IsNullOrEmpty(eMail))
				return false;

			const string regularExpression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
								 @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
								 @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

			return Regex.IsMatch(eMail, regularExpression);
		}
}

Related Tutorials