Validate is a valid email address or not. - CSharp System.Text.RegularExpressions

CSharp examples for System.Text.RegularExpressions:Match Email

Description

Validate is a valid email address or not.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using System;//from   w w  w. ja v  a 2s  . co m

public class Main{
        /// <summary>
        /// Validate is a valid email address or not.
        /// </summary>
        /// <param name="inputEmail">Email address to check.</param>
        /// <returns>True if valid otherwise false.</returns>
        public static bool IsEmail(string inputEmail)
        {
            //Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
            //return emailregex.IsMatch(val);

            string text1 = @"^([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})(\]?)$";
            Regex regex1 = new Regex(text1);
            if (regex1.IsMatch(inputEmail))
                return true;
            else
                return false;
        }
}

Related Tutorials