PC Review


Reply
Thread Tools Rate Thread

Code for validating email address in C# - validate email

 
 
Henrik Nyberg
Guest
Posts: n/a
 
      8th Aug 2003
Here's a small method for validating email in C#. It may save you some time..

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;
}
}
 
Reply With Quote
 
 
 
 
Chris R. Timmons
Guest
Posts: n/a
 
      8th Aug 2003
(E-Mail Removed) (Henrik Nyberg) wrote in
news:(E-Mail Removed):

> Here's a small method for validating email in C#. It may save
> you some time..
>
> 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;
> }
> }


Henrik,

You could shorten your code by removing the check for the @ symbol.
It's redundant since the regex will fail if the supplied email
address has more than one @ symbol.

Also, your regex is too restrictive. It will return false for a
valid email address like (E-Mail Removed).

Try this:

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}
else
{
return Regex.IsMatch(sEmail, @"
^
[-a-zA-Z0-9][-.a-zA-Z0-9]*
@
[-.a-zA-Z0-9]+
(\.[-.a-zA-Z0-9]+)*
\.
(
com|edu|info|gov|int|mil|net|org|biz|
name|museum|coop|aero|pro
|
[a-zA-Z]{2}
)
$",
RegexOptions.IgnorePatternWhitespace);
}
}


Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I add a code to validate an email address? Seimanidis Microsoft Frontpage 1 10th May 2008 09:08 AM
How can I validate an email address prior to sending an email? =?Utf-8?B?SWR5IENsYXJl?= Microsoft Outlook Discussion 8 30th Jun 2006 03:14 PM
How can I validate an email address prior to sending an email? =?Utf-8?B?SWR5IENsYXJl?= Microsoft Outlook Discussion 0 29th Jun 2006 02:33 PM
validating an email address =?Utf-8?B?SnVsaWU=?= Microsoft Access Form Coding 1 6th Oct 2004 01:04 PM
Validating an Email Address =?Utf-8?B?TmFuY3k=?= Microsoft Access Form Coding 1 29th Feb 2004 05:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:28 AM.