I want to check if email address is valid or not.
How can an email address be validated in VB using Regular Expressions?
([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}) I want to check if email address is valid or not.
How can an email address be validated in VB using Regular Expressions?
([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}) MSDN Article: How to: Verify That Email are in Valid E-Mail Format
This example method calls the Regex.IsMatch(String, String) method to verify that the string conforms to a regular expression pattern.
Function IsValidEmailFormat(ByVal s As String) As Boolean Return Regex.IsMatch(s, "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$") End Function Function validateEmail(emailAddress) As Boolean Dim email As New Regex("([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})") If email.IsMatch(emailAddress) Then Return True Else Return False End If Return email.IsMatch(emailAddress)This is my approach without messing with RegEx, taken from my ElektroKit API:
''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Validates a mail address. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code> ''' Dim isValid As Boolean = ValidateMail("[email protected]") ''' Dim isValid As Boolean = ValidateMail("mailto:[email protected]") ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="address"> ''' The mail address. ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' <see langword="True"/> if the mail address is valid, <see langword="False"/> otherwise. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepThrough> Public Shared Function ValidateMail(ByVal address As String) As Boolean If Not address.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase) Then address = address.Insert(0, "mailto:") End If If Not Uri.IsWellFormedUriString(address, UriKind.Absolute) Then Return False End If Dim result As Uri = Nothing If Uri.TryCreate(address, UriKind.Absolute, result) Then Return (result.Scheme = Uri.UriSchemeMailto) Else Return False End If End Function Note: I didn't performed a deep test with huge lists of emails.
If you preffer a regular expression, then this is my own following Wikipedia especifications, it gives me better results than other "universal" expressions, however, this expression is not perfectlly constructed, it doesn't avoid for example the double-dot conflict specified in Wikipedia, but we assume that will not encounter strange naming exceptions like that.
This expression never gave me a false positive (by the moment), but as any other Regex expr, use it at your own risk:
''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' A pattern that matches an e-mail address. ''' <para></para> ''' ''' For Example: ''' <para></para> ''' <c>[email protected]</c> ''' ''' </summary> ''' ---------------------------------------------------------------------------------------------------- Public Const EMail As String = "^(?<address>(?<local>[\d\w!#\$%\&'\*\+-/=\?\^_`\\\|~""\(\),:;\<\>@\[\]\{\}\.]+)@(?<domain>[\d\w-\.]+)\..+)$"