Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: beginner
Sep 14, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

IsValidEmail - Validate an email address

Function IsValidEmail(ByVal Value As String, Optional ByVal MaxLength As _
    Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean
    If Value Is Nothing OrElse Value.Length = 0 Then
        ' rule out the null string case
        Return Not IsRequired
    ElseIf Value.Length > MaxLength Then
        ' rule out values that are longer than allowed
        Return False
    End If

    ' search invalid chars
    If Not System.Text.RegularExpressions.Regex.IsMatch(Value, _
        "^[-A-Za-z0-9_@.]+$") Then Return False

    ' search the @ char
    Dim i As Integer = Value.IndexOf("@"c)
    ' there must be at least three chars after the @
    If i <= 0 Or i >= Value.Length - 3 Then Return False
    ' ensure there is only one @ char
    If Value.IndexOf("@"c, i + 1) >= 0 Then Return False

    ' check that the domain portion contains at least one dot
    Dim j As Integer = Value.LastIndexOf("."c)
    ' it can't be before or immediately after the @ char
    If j < 0 Or j <= i + 1 Then Return False

    ' if we get here the address if validated
    Return True
End Function

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date