by David Gabrielsen (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
This code returns a boolean expression that declares if a string is a valid email address or not. It returns true if the string is valid, false if not
Inputs
email as string
Code Returns
CheckIfEmail as boolean
Side Effects
it doesn't checks if the string actually is an email address, only if it is a valid email address.
Public Function checkIfEmail(email As String) As Boolean
Dim i As Integer
Dim char As String
Dim c() As String
'checks if the string has the standard email pattern:
If Not email Like "*@*.*" Then
checkIfEmail = False
Exit Function
End If
'splits the email-string with a "." delimeter and returns the subtring in the c-string array
c = Split(email, ".", -1, vbBinaryCompare)
'checks if the last substring has a length of either 2 or 3
If Not Len(c(UBound(c))) = 3 And Not Len(c(UBound(c))) = 2 Then
checkIfEmail = False
Exit Function
End If
'steps through the last substring to see if it contains anything else unless characters from a to z
For i = 1 To Len(c(UBound(c))) Step 1
char = Mid(c(UBound(c)), i, 1)
If Not (LCase(char) <= Chr(122)) Or Not (LCase(char) >= Chr(97)) Then
checkIfEmail = False
Exit Function
End If
Next i
'steps through the whole email string to see if it contains any special characters:
For i = 1 To Len(email) Step 1
char = Mid(email, i, 1)
If (LCase(char) <= Chr(122) And LCase(char) >= Chr(97)) _
Or (char >= Chr(48) And char <= Chr(57)) _
Or (char = ".") _
Or (char = "@") _
Or (char = "-") _
Or (char = "_") Then
checkIfEmail = True
Else
checkIfEmail = False
Exit Function
End If
Next i
End Function