Results 1 to 3 of 3

Thread: check if textbox.text contains at least...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I have a text box whereby I need to check to make sure that the user typed in a minimum pattern requirement.

    The text box needs to at the minimum contain 1 character (either alpha or numeric) followed by a / and then followed by at least 1 more character (alpha or numeric). So, at the minimum, the text needs to look like:

    a/a or 1/1 or A/a or 4/5 etc...

    but can also look like:

    asdfkadgjakldf/asdfadjfkladfj/asdfajdfkj etc...

    just as long as there is at least 1 forward slash immediately preceded by character and followed by a character..

    any help would be apprecaited..

    dan

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    This may not be the best solution, but it works:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox blValidateText(Text1.Text)
    End Sub
    
    Private Function blValidateText(str As String) As Boolean
        Dim stLeft As String
        Dim stRight As String
        Dim I As Integer
        Dim strPrev As String: strPrev = ""
        
        If InStr(1, str, "/") = 0 Or Len(str) < 3 Or Left(str, 1) = "/" Or Right(str, 1) = "/" Then
            blValidateText = False
            Exit Function '//Exit function (no / so there is no point in checking any further)
        End If
        
        '//Get characters preceding and following the FIRST forward slash
        stLeft = Mid(str, InStr(1, str, "/") - 1, 1)
        stRight = Mid(str, InStr(1, str, "/") + 1, 1)
        
        For I = 1 To Len(str) '//We don't want two or more / in a row
            '//If there are two / in a row then exit the function
            If Mid(str, I, 1) = "/" And strPrev = "/" Then
                blValidateText = False
                Exit Function
            End If
            strPrev = Mid(str, I, 1)
        Next I
        
        '//Check if the character preceding the "/" is a valid character (letter or a number)
        If (UCase(stLeft) < "A" Or UCase(stLeft) > "Z") And Not IsNumeric(stLeft) Then
            blValidateText = False
            Exit Function
        End If
        
        '//Check if the character following the "/" is a valid character (letter or a number)
        If (UCase(stRight) < "A" Or UCase(stRight) > "Z") And Not IsNumeric(stRight) Then
            blValidateText = False
            Exit Function
        End If
        
        blValidateText = True
    End Function

  3. #3
    Addicted Member overhill's Avatar
    Join Date
    Mar 2000
    Location
    KS, United States
    Posts
    181

    Here it is

    This may not be the most effiecient code but it should work for you! Just add a textbox and a command button.

    Code:
    Private Sub Command1_Click()
        '-Initial check to see if a character even exists
        If Len(Text1.Text) < 1 Then
            Exit Sub
        End If
        '-Put text into variable
        Dim tmpText As String
        tmpText = Text1
        '-Search for /
        Dim BackSlashPos As Integer
        BackSlashPos = InStr(1, tmpText, "/")
        If BackSlashPos = 0 Then
            MsgBox "Missing Backslash: a/a"
        Else
            If Not Len(tmpText) > BackSlashPos Then
                MsgBox "No Character After Backslash: a/a"
            Else
                MsgBox "Correct Form!"
            End If
        End If
    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