Friday, February 19, 2010

how to check EmailID in C#

Create function:

public bool IsValidEmail(string email)
{
//regular expression pattern for valid email
//addresses, allows for the following domains:
//com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tv
string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(comeduinfogovintmilnetorgbiznamemuseumcoopaeroprotv[a-zA-Z]{2})$";
//Regular expression object
Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
//boolean variable to return to calling method
bool valid = false;
//make sure an email address was provided
if (string.IsNullOrEmpty(email))
{
valid = false;
}
else
{
//use IsMatch to validate the address
valid = check.IsMatch(email);
}
//return the value to the calling method
return valid;
}

Class Name:
using System.Text.RegularExpressions;

Use Function:

bool check=IsValidEmail(txtsearchtext.Text.ToString());
if(check == true)
{ Send Mail() }
else { Invalid Email}

No comments:

Post a Comment