I use this
@\"^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\"
regexp to validate the email
([\\w\\.\\-]+)
- this is f
here is our Regex for this case:
@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$",
there are three parts, which are checcked. the last one is propably the one you need. the specific term {2,6} indicates you the min/max length of the TLD at the end. HTH
public static bool ValidateEmail(string str)
{
return Regex.IsMatch(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
I use the above code to validate the email address.
public bool VailidateEntriesForAccount()
{
if (!(txtMailId.Text.Trim() == string.Empty))
{
if (!IsEmail(txtMailId.Text))
{
Logger.Debug("Entered invalid Email ID's");
MessageBox.Show("Please enter valid Email Id's" );
txtMailId.Focus();
return false;
}
}
}
private bool IsEmail(string strEmail)
{
Regex validateEmail = new Regex("^[\\W]*([\\w+\\-.%]+@[\\w\\-.]+\\.[A-Za-z] {2,4}[\\W]*,{1}[\\W]*)*([\\w+\\-.%]+@[\\w\\-.]+\\.[A-Za-z]{2,4})[\\W]*$");
return validateEmail.IsMatch(strEmail);
}
I think @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
should work.
You need to write it like
string email = txtemail.Text;
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
Response.Write(email + " is correct");
else
Response.Write(email + " is incorrect");
Be warned that this will fail if:
There is a subdomain after the @
symbol.
You use a TLD with a length greater than 3, such as .info
Try the Following Code:
using System.Text.RegularExpressions;
if (!Regex.IsMatch(txtEmail.Text, @"^[a-z,A-Z]{1,10}((-|.)\w+)*@\w+.\w{3}$"))
MessageBox.Show("Not valid email.");
string patternEmail = @"(?<email>\w+@\w+\.[a-z]{0,3})";
Regex regexEmail = new Regex(patternEmail);