电子邮件格式检查VB

时间:2016-03-22 04:42:27

标签: .net regex vb.net email email-validation

我想检查电子邮件地址是否有效。

如何使用正则表达式在VB中验证电子邮件地址?

    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

3 个答案:

答案 0 :(得分:2)

MSDN文章:How to: Verify That Email are in Valid E-Mail Format

此示例方法调用Regex.IsMatch(String,String)方法以验证字符串是否符合正则表达式模式。

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

答案 1 :(得分:1)

Suit

答案 2 :(得分:0)

这是我的方法,没有弄乱 ElektroKit API 中的 RegEx

enter image description here

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Validates a mail address.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim isValid As Boolean = ValidateMail("Address@Gmail.com")
''' Dim isValid As Boolean = ValidateMail("mailto:Address@Gmail.com")
''' </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

注意:我没有使用大量电子邮件进行深度测试。

如果您在 this is my own 之后使用正则表达式, Wikipedia especifications ,它会比其他&#34; universal&#给我更好的结果34;但是,这个表达式并不是完美构造的,它不会避免例如 Wikipedia 中指定的双点冲突,但我们假设不会遇到像这样的奇怪的命名异常。

这个表达从来没有给我一个误报(目前),但与任何其他正则表达式 expr一样,使用它需要您自担风险:

enter image description here

''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' A pattern that matches an e-mail address.
''' <para></para>
''' 
''' For Example:
''' <para></para>
''' <c>local@domain.com</c>
''' 
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Const EMail As String =
    "^(?<address>(?<local>[\d\w!#\$%\&'\*\+-/=\?\^_`\\\|~""\(\),:;\<\>@\[\]\{\}\.]+)@(?<domain>[\d\w-\.]+)\..+)$"