smart developer’s blog

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

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]

Advertisements

Written by smartdev

March 19, 2009 at 1:13 pm

Posted in .Net

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: