C# Code Snippet - Validate Email Address

C# Code Snippet - Validate Email Address

C# Code Snippet - Validate Email Address

(C-Sharp) C# code snippet to validate given email address. IsValidEmail function validate email address. Function return TRUE if valid email address found, if not function will return FALSE.

Bookmark:

C# Code Snippet - Validate Email Address

This .Net C# code snippet validate given email address. This function uses regular expression to match string pattern to validate email address.

public bool IsValidEmail(string Email)
{
    string strRegex = @"^([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})(\]?)$";
    System.Text.RegularExpressions.Regex _Regex = new System.Text.RegularExpressions.Regex(strRegex);
    if (_Regex.IsMatch(Email))
        return (true);
    else
        return (false);
}


C# Keywords Used:

  • Regex
  • IsMatch

Code Snippet Information:

  • Applies To: .Net, C#, Email Validation, Regular Expression
  • Programming Language : C#

External Resources:

Siddhartha Sharma :: September 03-2010 :: 06:55 AM

Sir,
Your Code checks only the regular expression of the Email... which can be checked on client Side..... But How we find tht Email is valid on the server or not..................
as "asasjdohaiugfdyfgyugsd@sdf.com" gives valid from ur code while Domain name is not valid. You hav any code to validate tht type of email.

Leave a comment