Results 1 to 5 of 5

Thread: [RESOLVED] Checking text box for @ symbol

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    91

    Resolved [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

  2. #2
    Addicted Member Piller's Avatar
    Join Date
    Oct 2004
    Location
    california
    Posts
    177

    Re: Checking text box for @ symbol

    VB Code:
    1. If InStr(Text1.Text, "@") > 0 Then
    2.     'code
    3. 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.
    Are we alive or just breathing?

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    1. Dim nPos As Long
    2. nPos = InStr(Text1.Text, "@")
    3. If nPos Then
    4.     MsgBox "@-symbol found at position " & nPos
    5. End If

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checking text box for @ symbol

    Try this from Karl Moore
    VB Code:
    1. Private Function IsEMailAddress(ByVal sEmail As String, Optional ByRef sReason As String) As Boolean
    2.  'code by Karl Moore
    3.  
    4.     Dim sPreffix As String
    5.     Dim sSuffix As String
    6.     Dim sMiddle As String
    7.     Dim nCharacter As Integer
    8.     Dim sBuffer As String
    9.  
    10.     sEmail = Trim(sEmail)
    11.  
    12.     If Len(sEmail) < 8 Then
    13.         IsEMailAddress = False
    14.         sReason = "Too short"
    15.         Exit Function
    16.     End If
    17.  
    18.  
    19.     If InStr(sEmail, "@") = 0 Then
    20.         IsEMailAddress = False
    21.         sReason = "Missing the @"
    22.         Exit Function
    23.     End If
    24.  
    25.  
    26.     If InStr(InStr(sEmail, "@") + 1, sEmail, "@") <> 0 Then
    27.         IsEMailAddress = False
    28.         sReason = "Too many @"
    29.         Exit Function
    30.     End If
    31.  
    32.  
    33.     If InStr(sEmail, ".") = 0 Then
    34.         IsEMailAddress = False
    35.         sReason = "Missing the period"
    36.         Exit Function
    37.     End If
    38.  
    39.     If InStr(sEmail, "@") = 1 Or InStr(sEmail, "@") = Len(sEmail) Or _
    40.         InStr(sEmail, ".") = 1 Or InStr(sEmail, ".") = Len(sEmail) Then
    41.         IsEMailAddress = False
    42.         sReason = "Invalid format"
    43.     Exit Function
    44.  
    45. End If
    46.  
    47.  
    48. For nCharacter = 1 To Len(sEmail)
    49.     sBuffer = Mid$(sEmail, nCharacter, 1)
    50.     If Not (LCase(sBuffer) Like "[a-z]" Or sBuffer = "@" Or _
    51.     sBuffer = "." Or sBuffer = "-" Or sBuffer = "_" Or _
    52.     IsNumeric(sBuffer)) Then: IsEMailAddress = _
    53.     False: sReason = "Invalid character": Exit Function
    54. Next nCharacter
    55.  
    56. nCharacter = 0
    57.  
    58. On Error Resume Next
    59.  
    60. sBuffer = Right(sEmail, 4)
    61. If InStr(sBuffer, ".") = 0 Then GoTo TooLong:
    62. If Left(sBuffer, 1) = "." Then sBuffer = Right(sBuffer, 3)
    63. If Left(Right(sBuffer, 3), 1) = "." Then sBuffer = Right(sBuffer, 2)
    64. If Left(Right(sBuffer, 2), 1) = "." Then sBuffer = Right(sBuffer, 1)
    65.  
    66.  
    67. If Len(sBuffer) < 2 Then
    68.     IsEMailAddress = False
    69.     sReason = "Suffix too short"
    70.     Exit Function
    71. End If
    72.  
    73. TooLong:
    74.  
    75. If Len(sBuffer) > 3 Then
    76.     IsEMailAddress = False
    77.     sReason = "Suffix too long"
    78.     Exit Function
    79. End If
    80.  
    81. sReason = Empty
    82. IsEMailAddress = True
    83.  
    84. End Function
    85.  
    86. Private Sub Command1_Click()
    87. Dim IsValid As Boolean
    88. Dim InvalidReason As String
    89. IsValid = IsEMailAddress(Text1.Text, InvalidReason)
    90. If IsValid = True Then
    91.    MsgBox "Valid EMail address format"
    92. Else
    93.    MsgBox "Invalid format, the reason given is: " & InvalidReason
    94. End If
    95. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width