devxlogo

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 = 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 ' if we get here the address if validated    Return TrueEnd Function

devx-admin

Share the Post: