Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do you make sure there is atleast two characters between the @ and the . character
Posted
Comments

Hi,

Example:

C#
string emailAddr = "MyEmail@live.com";
bool isValid = IsEMailAddrValid(emailAddr);
if (isValid)
{
  // popup message box?
}
// Do anything ?



C#
private bool IsEMailAddrValid(string emailAddr)
{
   bool result = true;
   int count = emailAddr.Count(f => f == '@');
   if (count == 0)
   {
       return false;
   }
   if (count > 1)
   {
      return false;
   }
   count = emailAddr.Count(f => f == '.');
   if (count == 0)
   {
      return false;
   }
   if (count > 1)
   {
      return false;
   }
   string theChar = emailAddr.Split('@')[1].ToString();
   int theCharLen = theChar.Split('.')[0].ToString().Length;
   if (theCharLen < 2)
   {
      result = false;
   }
   return result;
} 


Regards,
 
Share this answer
 
use this class

C#
using System.Text.RegularExpressions;

public static class Validator
{

    static Regex ValidEmailRegex = CreateValidEmailRegex();

    /// <summary>
    /// Taken from http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
    /// </summary>
    /// <returns></returns>
    private static Regex CreateValidEmailRegex()
    {
        string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
            + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
            + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

        return new Regex(validEmailPattern, RegexOptions.IgnoreCase);
    }

    internal static bool EmailIsValid(string emailAddress)
    {
        bool isValid = ValidEmailRegex.IsMatch(emailAddress);

        return isValid;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900