VB icon

Check if a string is an email address

Email
Submitted on: 1/6/2015 1:28:00 PM
By: David Gabrielsen (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: VB 5.0, VB 6.0
Views: 446
 
     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
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Check if a string is an email address
' Description: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
' By: David Gabrielsen (from psc cd)
'
' Inputs:email as string
'
' 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


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.