IsValidEmail – Validate an email address

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 TrueEnd Function


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist