Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
do anyone have function or any shortcut to validate e-mail address (@ sign and .com). I need to check if that mail address is having domain name (like .com .uk) if not give return most of ur examples having only @ validation
Posted
Updated 16-May-10 6:26am
v2

You could use a Regex, but you can also just try to instantiate a System.Net.Mail.MailAddress( string address ) object.

The constructor calls the internal MailAddressParser class and throws an exception if the address is illegal.

Nick
 
Share this answer
 
Comments
#realJSOP 19-May-10 6:13am    
IMHO, Forcing the code to throw an exception is bad form. I try to restrict exception handling to *unexpected* errors.
Wow, you would have got a quicker result if you had just used google yourself!

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;
}
}


from here (there is other examples out there also!);
http://bytes.com/topic/c-sharp/answers/225262-code-validating-email-address-c-validate-email[^]
 
Share this answer
 
Comments
Kasunmit 16-May-10 12:06pm    
thnx for ur help friend ... i used google but most of them are having validations only for @ sing not for .com
I absolutely abhor using regex, but it really is the best tool for the job in this case:


//--------------------------------------------------------------------------------
public static bool ValidEmailAddress(string addy)
{
    bool valid = true;
    string pattern = @"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
    Regex regEx = new Regex(pattern);
    valid = (regEx.IsMatch(addy));
    return valid;
}
 
Share this answer
 
v2
Comments
Christian Graus 16-May-10 12:20pm    
Regex is cool, what I love about regex is, it's so unintuitive, you can't just look at a regex and know what it does without thinking about it.
You can do it using Regular expressions.
C#
using System.Text; 
private bool IsValidEmail(string email)        
{            
    string emailRegEx = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|"
                      + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*([-][a-z|0-9]+)*\.([a-z]" 
                      + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
    RegularExpressions.Match match = 
    RegularExpressions.Regex.Match(email, emailRegEx, RegularExpressions.RegexOptions.IgnoreCase);            
    return match.Success;  
}

Read this for more info:
MSDN: How To: Use Regular Expressions to Constrain Input in ASP.NET[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900