smart developer’s blog

This is a C# resource library! Free how to’s and best practices…

Archive for March 2009

Validate email address

leave a comment »

Here is some nice code to help you validate a email address in C#:

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
return false;

int nFirstAT = sEmail.IndexOf(‘@’);
int nLastAT = sEmail.LastIndexOf(‘@’);

if ((nFirstAT > 0) && (nLastAT == nFirstAT) && (nFirstAT < (sEmail.Length - 1))) { // address is ok regarding the single @ sign return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)")); } else { return false; } } [/sourcecode]

Written by smartdev

March 19, 2009 at 1:13 pm

Posted in .Net

Tagged with ,