title: Best Regular Expression for Email Format Validation with ASP.NET 3.5 Validation
body: <p>I've used both of the following Regular Expressions for testing for a valid email expression with ASP.NET validation controls. I was wondering which is the better expression from a performance standpoint, or if someone has better one.</p>
  <pre>
   - \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
   - ^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
  </pre>
  <p>I'm trying avoid the &quot;exponentially slow expression&quot; problem described on the <a href="https://web.archive.org/web/20140422045050/http://blogs.msdn.com/b/bclteam/archive/2005/02/10/370690.aspx" rel="nofollow noreferrer">BCL Team Blog</a>.</p>
  <p><strong>UPDATE</strong></p>
  <p>Based on feedback I ended up creating a function to test if an email is valid:</p>
  <pre><code>Public Function IsValidEmail(ByVal emailString As String, Optional ByVal isRequired As Boolean = False) As Boolean
      Dim emailSplit As String()
      Dim isValid As Boolean = True
      Dim localPart As String = String.Empty
      Dim domainPart As String = String.Empty
      Dim domainSplit As String()
      Dim tld As String
  
      If emailString.Length &gt;= 80 Then
          isValid = False
      ElseIf emailString.Length &gt; 0 And emailString.Length &lt; 6 Then
          'Email is too short
          isValid = False
      ElseIf emailString.Length &gt; 0 Then
          'Email is optional, only test value if provided
          emailSplit = emailString.Split(CChar(&quot;@&quot;))
  
          If emailSplit.Count &lt;&gt; 2 Then
              'Only 1 @ should exist
              isValid = False
          Else
              localPart = emailSplit(0)
              domainPart = emailSplit(1)
          End If
  
          If isValid = False OrElse domainPart.Contains(&quot;.&quot;) = False Then
              'Needs at least 1 period after @
              isValid = False
          Else
              'Test Local-Part Length and Characters
              If localPart.Length &gt; 64 OrElse ValidateString(localPart, ValidateTests.EmailLocalPartSafeChars) = False OrElse _
                 localPart.StartsWith(&quot;.&quot;) OrElse localPart.EndsWith(&quot;.&quot;) OrElse localPart.Contains(&quot;..&quot;) Then
                  isValid = False
              End If
  
              'Validate Domain Name Portion of email address
              If isValid = False OrElse _
                 ValidateString(domainPart, ValidateTests.HostNameChars) = False OrElse _
                 domainPart.StartsWith(&quot;-&quot;) OrElse domainPart.StartsWith(&quot;.&quot;) OrElse domainPart.Contains(&quot;..&quot;) Then
                  isValid = False
              Else
                  domainSplit = domainPart.Split(CChar(&quot;.&quot;))
                  tld = domainSplit(UBound(domainSplit))
  
                  ' Top Level Domains must be at least two characters
                  If tld.Length &lt; 2 Then
                      isValid = False
                  End If
              End If
          End If
      Else
          'If no value is passed review if required
          If isRequired = True Then
              isValid = False
          Else
              isValid = True
          End If
      End If
  
      Return isValid
  End Function
  </code></pre>
  <p>Notes:</p>
  <ul>
  <li>IsValidEmail is more restrictive about characters allowed then the RFC, but it doesn't test for all possible invalid uses of those characters</li>
  </ul>