what is the best regex to validate email.

i would like to validate it to be a valid email not ex : abcd@efgh.ijk

Try this code:

public static bool isValidEmail(string inputEmail)
{
   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})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}

thanks !! i'll try it and will let you know !

this accepts abcd@efgh.ijk !! it only checks the format of email ! :/

If you would like to validate the last part of the email string; this are the extensions (com, us, ...) i would suggest you to do a list of them, and then loop through them to check if the inserted one has any match, like here:

/// <summary>
/// method for determining is the user provided a valid email address
/// We use regular expressions in this check, as it is a more thorough
/// way of checking the address provided
/// </summary>
/// <param name="email">email address to validate</param>
/// <returns>true is valid, false if not valid</returns>
public bool IsValidEmail(string email)
{
    //regular expression pattern for valid email
    //addresses, allows for the following domains:
    //com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tv
    string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
    //Regular expression object
    Regex check = new Regex(pattern,RegexOptions.IgnorePatternWhitespace);
    //boolean variable to return to calling method
    bool valid = false;

    //make sure an email address was provided
    if (string.IsNullOrEmpty(email))
    {
        valid = false;
    }
    else
    {
        //use IsMatch to validate the address
        valid = check.IsMatch(email);
    }
    //return the value to the calling method
    return valid;
}

thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.