Solved

Extract letters from a string contaning numbers

Posted on 2014-07-25
12
139 Views
Last Modified: 2014-07-25
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 ?


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

Open in new window

0
Comment
Question by:isnoend2001
  • 7
  • 5
12 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220437
This will result in just the letters in Final. If you just want numbers then remove the Not.

      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

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220440
I'm sorry but I see I most likely misunderstood your requirement.

What things would make the input invalid?
0
 

Author Comment

by:isnoend2001
ID: 40220470
Thanks martin liss
If the user only typed numbers and no letters it could be valid or

 StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
the output would also need the letters "yjkkgdky" and the number value  5000 to 9900
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40220482
Dim Index As Long

    Dim Digit As String
    Dim strLetters As String
    Dim strNumbers As String
    Dim Count As Long
    
    For Index = 1 To Len(TextBox1.Text)
      If IsNumeric(Mid$(TextBox1.Text, Index, 1)) Then
          'Count = Count + 1 What do you want to count?
          strNumbers = strNumbers & Mid$(TextBox1.Text, Index, 1)
       Else
          strLetters = strLetters & Mid$(TextBox1.Text, Index, 1)
       End If
    Next

    MsgBox strNumbers
    MsgBox strLetters

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220513
Or perhaps something like this.

    Const Valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

    For Index = 1 To Len(TextBox1.Text)
        If InStr(1, Mid$(TextBox1.Text, Index, 1), Valid) = 0 Then
            MsgBox "Invalid character '" & Mid$(TextBox1.Text, Index, 1) & "' found"
            Exit Sub
       End If
    Next

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 40220525
Thanks MartinLiss
I added this Function from your 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
and checked it against "yjkkgdky"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220535
What about "yjk#k@gdky" ?
0
 

Author Comment

by:isnoend2001
ID: 40220538
No your last post allows any numbers or letters.
I need to restrict the input to values  to be between 5000 to 9900 and must contain
the letters: "yjkkgdky"
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220545
Ok then I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
 

Author Comment

by:isnoend2001
ID: 40220552
What does this mean  "yjk#k@gdky"
Hope you can answer my next question i am going to post. its regarding this same routine
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40220556
I assumed that someone was entering a value containing characters like "@", "#", "$", etc in a textbox and if so your code in post ID: 40220525 would treat those as non-numeric.
0
 

Author Comment

by:isnoend2001
ID: 40220569
thats ok if  "@", "#", "$" it would be invalid mean nothing
The input must contain "yjkkgdky" and the value between 5000 and 9900
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 36
Visual Studio search word table and return Cell index 8 49
how to know if my Checkbox is True in VB6.0? 9 41
Child Form in front 4 39
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now