Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check for valid email address?

Status
Not open for further replies.

089704318

IS-IT--Management
May 22, 2006
12
VN
Hi all,
If you know how to valid email address in C# please help to show me that?

Thanks,
 
This simple function will help you in some way:
Code:
public static bool checkForValidEmailAddr(string strEmail)
{
  if (strEmail == null)
    {
      return false;
    }

  int indFirst = strEmail.IndexOf('@');
  int indLast = strEmail.LastIndexOf('@');

  if ( (indFirst > 0) && (indLast == indFirst) && (indFirst < (strEmail.Length - 1)) )
  {
     return (Regex.IsMatch(strEmail, @"(\w+)@(\w+)\.(\w+)"));
  }
  else
  {
    return false;
  }
}

Sharing the best from my side...

--Prashant--
 
do not forget that the Regex class lives in the System.Test.RegularExpression namespace
 
I'm not sure why the extra code in Prashant's example.

Your function could just return the result of a RegEx check. You can get very thorough e-mail regexes at regexlib.com

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
As far as anyone knows, there's no single regex that will validate with 100% certainty, an email address.

Take a look at:

There's 10 or more sample regex expressions there, and none of them are perfect (one claims RFC-2822 compliance, but isn't). The part where almost all of them fall down is on country-code validation. Not all email addresses end in .com, .net, or .org. :-|

So you'll most likely need to write some code that will apply several different tests to see if a string passed to you is a valid address per RFC-2822.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
isn't the difficulty here that we're confusing two quite different problems.

1. compliance: conforms to the naming rules
2. validity: is a reachable address

someone@server.smtp is a compliant email address, but is invalid because the smtp tld does not exist.

no regexp can check for validity. as you move left on the address you can be less certain of validity. you could factor in all the tld info but this is not static; two ways to do it would be either to keep a database of possible tlds or to extract them and poll to see if they answer, both of whuich are somewhat suboptimal.

i have a feeling this is a case of letting go, and trusting the user. if they've entered something that is compliant, take their word that it is valid.

</2c>


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top