Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how do you make sure there is atleast two characters between the @ and the . character
Posted 8-Nov-12 12:50pm
Comments
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use this class
 
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;
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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,
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid