Ugh.. could not figure this out. Can someone help me? How to check if an input string contains at least 1 numeric character?
Printable View
Ugh.. could not figure this out. Can someone help me? How to check if an input string contains at least 1 numeric character?
Try this: If myString Like "*[0-9]*" Then ...
Also like this: If myString Like "*#*" Then
An alternative:
Code:Dim MyString As String
MyString = "hasdsjljx d4 kdkja z " ' Sample string
For I = 48 To 57
If InStr(MyString, Chr$(I)) Then
MsgBox "MyString contains numeric character " & Chr$(I) & "."
Exit For
End If
Next
An alternative, but you don't drink bucks fizz when it's an alternative to champagne :-)
But Like does exist, hence an alternative might not be that useful. I'll concede that there's a chance that they're writing in an early enough version of VB (no idea which that'd be, of course) that doesn't have Like in it, so it's an alternative, but normally when you evolve (for instance from instr/chr to Like) you don't use devoluted methods and stick with the most recent unless you're at a disadvantage (much like how many people feel about VB6 and upgrading to NET but we won't go into that :-))
CD, it may not be the latest option but it's definitely clever. :thumb:
Well yes, a very astute observation.
Insults aside, Doc's code may not be the best solution for this topic but I don't think it or I deserve the ridicule that seems to flow quite freely from some. Personally, I will keep Doc's code in mind because the concept may find use elsewhere.
Here's a rule I try to live by.
Don't gratuitously insult people that are not facing and standing directly in front of you, because it's the trade mark of a Weasel.
I have to side with CDrive here. Lately, there has been sarcasm, flaming, degrading comments posted in responses to code freely provided.
If the code is wrong, then point out the errors without slamming the poster.
If the code is correct, point out a more efficient and/or easier method, but don't insult in the process.
There is absolutely nothing wrong with being courteous, remember we are all volunteers here and of different levels of experience.
With any language, many times there are multiple ways to say the same thing. It may not be the "best" way to say it, but if it isn't the wrong way, then maybe, just maybe, the end result will be that someone learned something new to them, whether they use it or not.
Edited: I'm sure some of us are rather friendly with others and the sarcasm may be enjoyed by both parties when aimed at each other. But if you aren't acquaintances, then sarcasm is almost always interpreted as derogatory/insult by the receiver.
I agree completely with LV.
Where does Code Doc's post fit in here, the original code was correct, but Doc's was not more efficient nor easier :-P
Anyway, my intent toward Doc was merely joking around, he's entitled to post alternative answers...I was just pointing out how useful the suggestion was considering the fact that Like had already been posted.
Like appears to have been introduced with VB4 in 1995. Select Case goes back to at least VB3 which was introduced in 1991, but I'm not sure what relevance that has because I don't see that mentioned anywhere else in the thread.
I only posted my alternative because I thought it was possible that OP had never used Like before and might be unfamiliar with it. Sorry to have upset so many apple carts by trying to be helpful. :sick:
nah select case goes way farther back.. to VB1 for DOS, and of course before that QuickBASIC and QBASIC.
your way will do the trick. another (bad) alternative of course would be:
Code:MyString$ = "blahblahblahblal345hjkb kdfg" 'sample string
containsanumber = False
For n = 1 To Len(MyString$)
tempval = Asc(Mid$(MyString$, n, 1))
If tempval > 47 And tempval < 58 Then containsanumber = True: Exit For
Next n
Begin using this code in this way...
Code:Dim Item1 As Byte
Public Sub Form_Load()
Item1 = "0"
End Sub