Return true if strIn is in valid e-mail format : Regex Email « Regular Expression « C# / CSharp Tutorial






using System;
using System.Text.RegularExpressions;

public class RegexUtilities
{
   public static void Main()
   {
      string[] emailAddresses = { "a.j@p.com", "d.j@s.p.com", 
                                  "j@m.p.com", "j.@s.p.com", 
                                  "j@p.com9" };
      foreach (string emailAddress in emailAddresses)
      {
         if (RegexUtilities.IsValidEmail(emailAddress))
            Console.WriteLine("Valid: {0}", emailAddress);
         else
            Console.WriteLine("Invalid: {0}", emailAddress);
      }                                            
   }
    
   public static bool IsValidEmail(string strIn)
   {

       return Regex.IsMatch(strIn, @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"); 
   }
}








17.3.Regex Email
17.3.1.Regex for Email
17.3.2.Return true if strIn is in valid e-mail format