Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Regular Expression for Email Format Validation with ASP.NET 3.5 Validation
    primarykey
    data
    text
    <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 "exponentially slow expression" problem described on the <a href="http://blogs.msdn.com/b/bclteam/archive/2005/02/10/370690.aspx" rel="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("@")) 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(".") = 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(".") OrElse localPart.EndsWith(".") OrElse localPart.Contains("..") Then isValid = False End If 'Validate Domain Name Portion of email address If isValid = False OrElse _ ValidateString(domainPart, ValidateTests.HostNameChars) = False OrElse _ domainPart.StartsWith("-") OrElse domainPart.StartsWith(".") OrElse domainPart.Contains("..") Then isValid = False Else domainSplit = domainPart.Split(CChar(".")) 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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload