Visual Studio .NET - validate an email address

Asked By sanjay k on 25-Aug-08 04:31 AM

I want to validate an email address with Regular expression. How to use it ?

Validate an email address - Kalit Sikka replied to sanjay k on 25-Aug-08 04:33 AM

public static bool isEmail(string inputEmail)
{
   inputEmail  = NulltoString(inputEmail);
   string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}

use this - Deepak Ghule replied to sanjay k on 25-Aug-08 04:33 AM

<asp:RegularExpressionValidator ID="regexpName" runat="server"    
                                    ErrorMessage="This expression does not validate."
                                    ControlToValidate="txtEmailId"    
                                    ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />

Use this RegularExpressionValidator and assign ControlToValidate to ur control name of emailid.

Use this - Atul Shinde replied to sanjay k on 25-Aug-08 04:34 AM

1)   Insert regular expression validator.

2)   Set ControltoValidate() property to particular E-Mail textbox field.

3)   Set Validation Expression of Regular Expression to Internet E-Mail Address Expression.

use as follows - Web Star replied to sanjay k on 25-Aug-08 04:36 AM

public static class TestEmail
{
  /// <summary>
  /// Regular expression, which is used to validate an E-Mail address.
  /// </summary>
  public const string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

  /// <summary>
  /// Checks whether the given Email-Parameter is a valid E-Mail address.
  /// </summary>
  /// <param name="email">Parameter-string that contains an E-Mail address.</param>
  /// <returns>True, wenn Parameter-string is not null and contains a valid E-Mail address;
  /// otherwise false.</returns>
  public static bool IsEmail(string email)
  {
     if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
     else return false;
  }
}

(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@ - matches the user name part of an E-Mail address. This expression part is matched as follows:

use this ValidationExpression - Deepak Ghule replied to sanjay k on 25-Aug-08 04:36 AM

Use this validation expression :

<asp:RegularExpressionValidator ID="regexpName" runat="server"    
                                    ErrorMessage="This expression does not validate."
                                    ControlToValidate="txtName"    
                                    ValidationExpression="="^[\w\.\-_]+@([\w\.\-_]+\.)+[A-Za-z]{2,4}$" />

ValidationExpression="^[\w\.\-_]+@([\w\.\-_]+\.)+[A-Za-z]{2,4}$ "


reply - Binny ch replied to sanjay k on 25-Aug-08 04:45 AM
Go to properties of the regular expression
properties--->validation Expression--->
Select this  the email address or wirte this
  \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*  and then in the control to validate property give the textbox name...

Hope its clear.

public static bool IsEmail(string Email)
    {
        string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
        Regex re = new Regex(strRegex);
        if (re.IsMatch(Email))
            return (true);
        else
            return (false);
    }
u can validate email by using this expression in javascript - Web Star replied to sanjay k on 25-Aug-08 05:02 AM
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}
Email validation using Regular Expression - SP replied to sanjay k on 25-Aug-08 05:19 AM

Email Validation using Regular Expression ::

public static class TestEmail
{
  /// <summary>
  /// Regular expression, which is used to validate an E-Mail address.
  /// </summary>
  public const string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

  /// <summary>
  /// Checks whether the given Email-Parameter is a valid E-Mail address.
  /// </summary>
  /// <param name="email">Parameter-string that contains an E-Mail address.</param>
  /// <returns>True, wenn Parameter-string is not null and contains a valid E-Mail address;
  /// otherwise false.</returns>
  public static bool IsEmail(string email)
  {
     if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
     else return false;
  }
}       


See the original article :: http://www.codeproject.com/KB/recipes/EmailRegexValidator.aspx

You can also try the following code ::

<%
Response.write isValidEmail("david@codetoad.com") & "<BR>"
Response.write isValidEmail("davidcodetoadcom")

Function isValidEmail(myEmail)
  dim isValidE
  dim regEx
 
  isValidE = True
  set regEx = New RegExp
 
  regEx.IgnoreCase = False
 
  regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
  isValidE = regEx.Test(myEmail)
 
  isValidEmail = isValidE
End Function%>


Hope it helps.

email regular expression - mv ark replied to sanjay k on 25-Aug-08 05:20 AM
There are numerous ways to write a regular expression to validate an email, some may be too generic while some may be too specific. While there are general rules about how email addresses can look, there may also be specific cases that you want to cover. This link returns 101 regular expressions to validate email addresses - http://regexlib.com/Search.aspx?k=email

Check & pick one that fits your requirement