【问题标题】:Email Format Check in VBVB中的电子邮件格式检查
【发布时间】:2016-03-22 04:42:26
【问题描述】:

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

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

([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
        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)
    【解决方案3】:

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

    ''' ----------------------------------------------------------------------------------------------------
    ''' <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 之后,它比其他“通用”表达式给我更好的结果,但是,这个表达式是构造不完美,它不能避免例如 Wikipedia 中指定的双点冲突,但我们假设不会遇到类似的奇怪命名异常。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2016-07-23
      • 2012-04-28
      • 2015-03-17
      • 2013-09-10
      • 2014-02-02
      相关资源
      最近更新 更多