5

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}) 
2
  • Possible duplicate of Validating e-mail with regular expression VB.Net Commented Mar 22, 2016 at 8:45
  • Shouldn't you simplify your question to, "how do I match a regular expression in VB.Net?" Commented Mar 22, 2016 at 8:52

3 Answers 3

3

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 
Sign up to request clarification or add additional context in comments.

Comments

1
 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 

2 Comments

Why you ask question before try once?
Why not just Return email.IsMatch(emailAddress)
0

This is my approach without messing with RegEx, taken from my ElektroKit API:

enter image description here

''' ---------------------------------------------------------------------------------------------------- ''' <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:

enter image description here

''' ---------------------------------------------------------------------------------------------------- ''' <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-\.]+)\..+)$" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.