Click here to Skip to main content
11,640,599 members (59,465 online)
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 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  
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  

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

  Print Answers RSS
0 Mika Wendelius 330
1 OriginalGriff 175
2 Afzaal Ahmad Zeeshan 130
3 CPallini 125
4 Sergey Alexandrovich Kryukov 94
0 Mika Wendelius 200
1 OriginalGriff 145
2 CPallini 115
3 jgakenhe 40
4 sasanka sekhar panda 30


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100