Compartilhar via


Example: Confirming Valid E-Mail Format

The following code example uses the static Regex.IsMatch method to verify that a string is in valid e-mail format. The IsValidEmail method returns true if the string contains a valid e-mail address and false if it does not, but takes no other action. You can use IsValidEmail to filter out e-mail addresses containing invalid characters before your application stores the addresses in a database or displays them in an ASP.NET page.

Function IsValidEmail(strIn As String) As Boolean
    ' Return true if strIn is in valid e-mail format.
    Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
End Function
[C#]
bool IsValidEmail(string strIn)
{
    // Return true if strIn is in valid e-mail format.
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 
}

See Also

.NET Framework Regular Expressions