[RESOLVED] Checking text box for @ symbol
Can anyone please tell me how i check a text box for the @ symbol?
I have a text box that an email address is to be entered and I want to check that the user is entering a valid email address, not just some random text.
I am pretty sure I could use Instr but i've only ever used it once and i didn't really understand it :)
Re: Checking text box for @ symbol
VB Code:
If InStr(Text1.Text, "@") > 0 Then
'code
End If
InStr returns a number if its there if not its 0. This checks to see if it returns a value greater then zero.
Re: Checking text box for @ symbol
Yes you should use InStr. It returns the position of where it has found one string in another... which of course could be 0 if the string is not found.
VB Code:
Dim nPos As Long
nPos = InStr(Text1.Text, "@")
If nPos Then
MsgBox "@-symbol found at position " & nPos
End If
Re: Checking text box for @ symbol
Quote:
Originally Posted by AdRock952
Can anyone please tell me how i check a text box for the @ symbol?
I have a text box that an email address is to be entered and I want to check that the user is entering a valid email address, not just some random text.
I am pretty sure I could use Instr but i've only ever used it once and i didn't really understand it :)
This thread among others that I found when I did an Advanced Search for validate email might help you.
Re: Checking text box for @ symbol
Try this from Karl Moore
VB Code:
Private Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean
'code by Karl Moore
Dim sPreffix As String
Dim sSuffix As String
Dim sMiddle As String
Dim nCharacter As Integer
Dim sBuffer As String
sEmail = Trim(sEmail)
If Len(sEmail) < 8 Then
IsEMailAddress = False
sReason = "Too short"
Exit Function
End If
If InStr(sEmail, "@") = 0 Then
IsEMailAddress = False
sReason = "Missing the @"
Exit Function
End If
If InStr(InStr(sEmail, "@") + 1, sEmail, "@") <> 0 Then
IsEMailAddress = False
sReason = "Too many @"
Exit Function
End If
If InStr(sEmail, ".") = 0 Then
IsEMailAddress = False
sReason = "Missing the period"
Exit Function
End If
If InStr(sEmail, "@") = 1 Or InStr(sEmail, "@") = Len(sEmail) Or _
InStr(sEmail, ".") = 1 Or InStr(sEmail, ".") = Len(sEmail) Then
IsEMailAddress = False
sReason = "Invalid format"
Exit Function
End If
For nCharacter = 1 To Len(sEmail)
sBuffer = Mid$(sEmail, nCharacter, 1)
If Not (LCase(sBuffer) Like "[a-z]" Or sBuffer = "@" Or _
sBuffer = "." Or sBuffer = "-" Or sBuffer = "_" Or _
IsNumeric(sBuffer)) Then: IsEMailAddress = _
False: sReason = "Invalid character": Exit Function
Next nCharacter
nCharacter = 0
On Error Resume Next
sBuffer = Right(sEmail, 4)
If InStr(sBuffer, ".") = 0 Then GoTo TooLong:
If Left(sBuffer, 1) = "." Then sBuffer = Right(sBuffer, 3)
If Left(Right(sBuffer, 3), 1) = "." Then sBuffer = Right(sBuffer, 2)
If Left(Right(sBuffer, 2), 1) = "." Then sBuffer = Right(sBuffer, 1)
If Len(sBuffer) < 2 Then
IsEMailAddress = False
sReason = "Suffix too short"
Exit Function
End If
TooLong:
If Len(sBuffer) > 3 Then
IsEMailAddress = False
sReason = "Suffix too long"
Exit Function
End If
sReason = Empty
IsEMailAddress = True
End Function
Private Sub Command1_Click()
Dim IsValid As Boolean
Dim InvalidReason As String
IsValid = IsEMailAddress(Text1.Text, InvalidReason)
If IsValid = True Then
MsgBox "Valid EMail address format"
Else
MsgBox "Invalid format, the reason given is: " & InvalidReason
End If
End Sub