Results 1 to 5 of 5

Thread: [RESOLVED] Extract letters from a string contaning numbers

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Resolved [RESOLVED] Extract letters from a string contaning numbers

    i have a regristraction routine that check for valid input
    I thought the following would work, but if the user only entered numbers and no letters
    it could be fooled, how can i make sure the proper letters are input ?


    Code:
    Private Sub Command5_Click()
        Dim Success As Boolean
        Dim Value As String
        Dim StringEntered As String
        StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
        Value = GetNumbers(StringEntered)
        
      If Val(Value) >= 5000 And Val(Value) <= 9900 Then
      'now check if the correct letters are typed
        Success = True
      Else
        Success = False
      End If
        MsgBox Success
    
    End Sub
    Function GetNumbers(Value As String) As String
    'extract and return numbers from a string
        Dim Index As Long
        Dim Digit As String
        Dim Final As String
        Dim Count As Long
        
          Count = 1
          GetNumbers = space(Len(Value))
          For Index = 1 To Len(Value)
            Digit = Mid(Value, Index, 1)
            If Digit Like "[0-9]" Then
              Mid(GetNumbers, Count, 1) = Digit
              Count = Count + 1
            End If
          Next
        
          GetNumbers = Left(GetNumbers, Count - 1)
    End Function
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Extract letters from a string contaning numbers

    The ISNumeric() function would tell you if the string is all numeric

    You could also loop through the string and test each character to see if it is alpha or numeric or other

    It looks like you are just throwing away the none numeric characters so someone could enter random alpha characters combined with a few numbers and still be saw as valid.


    Then again in your existing code you could add an Else clause and increment a second counter when the character is not a digit then after the loop check the number on non digits that were entered.

  3. #3

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Extract letters from a string contaning numbers

    Thanks DataMiser, yes that is why i am asking the user could enter only numbers or numbers with any letters and it would be valid

    I need to also check to make sure the letters "yjkkgdky" are also entered. they would not need to be in the correct position
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Extract letters from a string contaning numbers

    Here pick whatever you need from this. It should get you going.

    Code:
    Private Sub Command1_Click()
    MsgBox CheckNumbers("yjk65kg12dsky")
    End Sub
    
    Function CheckNumbers(Value As String) As Boolean
    Dim TempString As String
    Dim LettersNeeded As String
    Dim LettersSplit() As String
    Dim i As Integer
    Dim j As Integer
    
      LettersNeeded = "y j k k g d k y"
      LettersSplit = Split(LettersNeeded, " ")
      TempString = Value
      
      For i = 0 To UBound(LettersSplit)
        For j = 1 To Len(TempString)
          If Mid(TempString, j, 1) = LettersSplit(i) Then
            TempString = Mid(TempString, 1, j - 1) & Mid(TempString, j + 1)
            Exit For
          ElseIf j = Len(TempString) Then
            MsgBox "Error did not find all letters."
            CheckNumbers = False
            Exit Function
          End If
        Next j
      Next i
        
      If IsNumeric(TempString) Then
        If CLng(TempString) >= 5000 And CLng(TempString) <= 9900 Then
          CheckNumbers = True
        Else
          MsgBox "Error number is too big or too small"
          CheckNumbers = False
        End If
      Else
        MsgBox "We found extra letters in the string/key"
        CheckNumbers = False
      End If
    
    End Function

  5. #5

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] Extract letters from a string contaning numbers

    Got this to work:
    Code:
    Function GetLetters(value As String)
       Dim Index As Long
        Dim Count As Long
        Dim Final As String
          For Index = 1 To Len(value)
           If Not IsNumeric(Mid$(value, Index, 1)) Then
              Count = Count + 1
              Final = Final & Mid$(value, Index, 1)
            End If
          Next
        GetLetters = Final
                                              
    End Function
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

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