shovels
Mar 21st, 2000, 09:23 PM
Hi,
Does anyone have sample code that demonstrates how to create a validation check to ensure email address is valid.
e.g Ensuring that it contains the '@' sign.
Any suggestions would be greatly appreciated.
Cheers.
JHausmann
Mar 21st, 2000, 10:05 PM
To check for an @, you could do:
if instr(1,text_to_check,"@")< 1 then 'no @ sign in the string
msgbox "You need to have an @ sign in your e-mail address"
end if
browner
Mar 23rd, 2000, 12:40 AM
Hi this is code I got from http://www.4guysfromrolla.com/webtech/051999-1.shtml I have added two functions that make it work with VB5 (it was wrote for 6)
Hope this helps
Function checkValidEmail(theAddress) As Boolean
'http://www.4guysfromrolla.com/webtech/051999-1.shtml
On Error GoTo ExitFalse
' checks for a valid email
' returns false for invalid addresses
Dim atCnt
checkValidEmail = True
' chk length
If Len(theAddress) < 5 Then
' a@b.c should be the shortest an
' address could be
checkValidEmail = False
' chk format
' has at least one "@"
ElseIf InStr(theAddress, "@") = 0 Then
checkValidEmail = False
' has at least one "."
ElseIf InStr(theAddress, ".") = 0 Then
checkValidEmail = False
' has no more than 3 chars after last "."
ElseIf Len(theAddress) - InStrRev(theAddress, ".") > 3 Then
checkValidEmail = False
' has no "_" after the "@"
ElseIf InStr(theAddress, "_") <> 0 And _
InStrRev(theAddress, "_") > InStrRev(theAddress, "@") Then
checkValidEmail = False
Else
' has only one "@"
atCnt = 0
For i = 1 To Len(theAddress)
If Mid(theAddress, i, 1) = "@" Then
atCnt = atCnt + 1
End If
Next
If atCnt > 1 Then
checkValidEmail = False
End If
' chk each char for validity
For i = 1 To Len(theAddress)
If Not IsNumeric(Mid(theAddress, i, 1)) And _
(LCase(Mid(theAddress, i, 1)) < "a" Or _
LCase(Mid(theAddress, i, 1)) > "z") And _
Mid(theAddress, i, 1) <> "_" And _
Mid(theAddress, i, 1) <> "." And _
Mid(theAddress, i, 1) <> "@" And _
Mid(theAddress, i, 1) <> "-" Then
checkValidEmail = False
End If
Next
End If
Exit Function
ExitFalse:
checkValidEmail = False
Exit Function
End Function
Public Function InStrRev(ByVal sIn As String, sFind As String, _
Optional nStart As Long = 1, Optional bCompare As _
VbCompareMethod = vbBinaryCompare) As Long
'http://support.microsoft.com/support/kb/articles/Q188/0/07.ASP
'THIS IS A VB6.0 FUNCTION SIMULATED FOR THE CHECKVALIDMAIL FUNCTION
'finds the last instance of a char in a string as
'opposed to instr which finds the first
Dim nPos As Long
sIn = StrReverse(sIn)
sFind = StrReverse(sFind)
nPos = InStr(nStart, sIn, sFind, bCompare)
If nPos = 0 Then
InStrRev = 0
Else
InStrRev = Len(sIn) - nPos - Len(sFind) + 2
End If
End Function
Public Function StrReverse(ByVal sIn As String) As String
'http://support.microsoft.com/support/kb/articles/Q188/0/07.ASP
'THIS IS A VB6.0 FUNCTION SIMULATED FOR THE CHECKVALIDMAIL FUNCTION
'supports the InStrRev function
Dim nC As Integer, sOut As String
For nC = Len(sIn) To 1 Step -1
sOut = sOut & Mid(sIn, nC, 1)
Next
StrReverse = sOut
End Function