|
| Author |
Message |
elena
Joined: 04 Oct 2007 Posts: 9
|
Posted: Tue Jan 02, 2007 6:34 pm Post subject: Help with "Like" operator |
|
|
Hi All,
I need to validate input in textbox.
Valid input should be from 05 to 99
Dim bCh as Boolen
Dim sStr as String = "04" (invalid input)
bCh = sStr Like "[0-9][0-9]"
Please, advice
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Ken Halter
Joined: 04 Oct 2007 Posts: 4150
|
Posted: Tue Jan 02, 2007 7:16 pm Post subject: Re: Help with "Like" operator |
|
|
"elena" wrote in message @microsoft.com...
> Hi All,
> I need to validate input in textbox.
> Valid input should be from 05 to 99
>
> Dim bCh as Boolen
> Dim sStr as String = "04" (invalid input)
>
> bCh = sStr Like "[0-9][0-9]"
>
> Please, advice
Doesn't use 'Like' but it's pretty easy to read...
'=========
Private Sub Command1_Click()
'Valid input should be from 05 to 99
Dim sTest As String
Dim dVal As Double
Dim bValid As Boolean
sTest = Text1.Text
If Len(sTest) = 2 Then
dVal = Val(sTest)
If dVal > 4 And dVal < 100 Then
bValid = True
End If
End If
MsgBox IIf(bValid, "", "Not ") & "Valid!"
End Sub
'=========
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm |
|
| Back to top |
|
 |
Ken Halter
Joined: 04 Oct 2007 Posts: 4150
|
Posted: Tue Jan 02, 2007 7:21 pm Post subject: Re: Help with "Like" operator |
|
|
"Ken Halter" wrote in message @TK2MSFTNGP06.phx.gbl...
> "elena" wrote in message
> @microsoft.com...
>> Hi All,
>> I need to validate input in textbox.
>> Valid input should be from 05 to 99
>>
>> Dim bCh as Boolen
>> Dim sStr as String = "04" (invalid input)
>>
>> bCh = sStr Like "[0-9][0-9]"
>>
>> Please, advice
>
>
> Doesn't use 'Like' but it's pretty easy to read...
> '=========
'More code ... still pretty easy to read.
Private Sub Command1_Click()
'Valid input should be from 05 to 99
Dim sTest As String
Dim dVal As Double
Dim bValid As Boolean
sTest = Text1.Text
If Len(sTest) = 2 Then
dVal = Val(sTest)
If dVal > 4 And dVal < 10 Then
If Left$(sTest, 1) = "0" Then
bValid = True
End If
ElseIf dVal > 9 And dVal < 100 Then
bValid = True
End If
End If
MsgBox IIf(bValid, "", "Not ") & "Valid!"
End Sub
> '=========
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Jan 03, 2007 12:51 am Post subject: Re: Help with "Like" operator |
|
|
> I need to validate input in textbox.
> Valid input should be from 05 to 99
>
> Dim bCh as Boolen
> Dim sStr as String = "04" (invalid input)
You can't assign a value in the Dim statement (in VB6 or earlier).
> bCh = sStr Like "[0-9][0-9]"
bCh = sStr Like "##" And Not sStr Like "0[0-4]"
Rick |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Jan 03, 2007 1:03 am Post subject: Re: Help with "Like" operator |
|
|
>> bCh = sStr Like "[0-9][0-9]"
>
> bCh = sStr Like "##" And Not sStr Like "0[0-4]"
Actually, in thinking about it, this slightly shorter version should work
also...
bCh = sStr Like "1#" Or sStr Like "0[5-9]"
Rick |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Jan 03, 2007 1:09 am Post subject: Re: Help with "Like" operator |
|
|
>>> bCh = sStr Like "[0-9][0-9]"
>>
>> bCh = sStr Like "##" And Not sStr Like "0[0-4]"
>
> Actually, in thinking about it, this slightly shorter version should work
> also...
>
> bCh = sStr Like "1#" Or sStr Like "0[5-9]"
Whoops (copied the wrong test code line)! I meant to post this line...
bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
But in looking at it now, I see we can shorten it even more...
bCh = sStr Like "[!0]#" Or sStr Like "0[5-9]"
Rick |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Jan 03, 2007 7:42 am Post subject: Re: Help with "Like" operator |
|
|
> I need to validate input in textbox.
> Valid input should be from 05 to 99
>
> Dim bCh as Boolen
> Dim sStr as String = "04" (invalid input)
>
> bCh = sStr Like "[0-9][0-9]"
Okay, for those of you who like my obfuscated one-liners, try to figure out
how this one works...
bCh = Abs(52 + (sStr Like "##") * Val(sStr)) < 48
It does **exactly** what the OP asked for... only two-digit values for sStr
between, and including, 05 and 99 return a value of True... all others
values of sStr return False.
Rick
Note to Elena
==============
You can ignore this response complete as it is NOT how you should do it; I
posted it solely for those people out there who like to see my "weirder"
one-liner solutions. |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Wed Jan 03, 2007 10:42 am Post subject: Re: Help with "Like" operator |
|
|
"Rick Rothstein (MVP - VB)" wrote in
message @TK2MSFTNGP04.phx.gbl
>>>> bCh = sStr Like "[0-9][0-9]"
>>>
>>> bCh = sStr Like "##" And Not sStr Like "0[0-4]"
>>
>> Actually, in thinking about it, this slightly shorter version should
>> work also...
>>
>> bCh = sStr Like "1#" Or sStr Like "0[5-9]"
>
> Whoops (copied the wrong test code line)! I meant to post this line...
>
> bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
>
> But in looking at it now, I see we can shorten it even more...
>
> bCh = sStr Like "[!0]#" Or sStr Like "0[5-9]"
That will allow things like "a0" to pass as True
--
Reply to the group so all can participate
VB.Net: "Fool me once..." |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Wed Jan 03, 2007 2:43 pm Post subject: Re: Help with "Like" operator |
|
|
>> Whoops (copied the wrong test code line)! I meant to post this line...
>>
>> bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
>>
>> But in looking at it now, I see we can shorten it even more...
>>
>> bCh = sStr Like "[!0]#" Or sStr Like "0[5-9]"
>
> That will allow things like "a0" to pass as True
Damn those last minute inspirations! Yep, you are right, [!0] is definitely
not the way to go.
Elena
=====
This one (the first way) still works though....
bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
Rick |
|
| Back to top |
|
 |
Tony Proctor
Joined: 04 Oct 2007 Posts: 1051
|
Posted: Thu Jan 04, 2007 3:27 pm Post subject: Re: Help with "Like" operator |
|
|
LOL!
I reckon you guys have had a bit too much of the Christmas (& New Year)
spirit
"...normal service will be resumed shortly"
Tony Proctor
"Rick Rothstein (MVP - VB)" wrote in
message news:%23Q4uTW0LHHA.4916@TK2MSFTNGP06.phx.gbl...
> >> Whoops (copied the wrong test code line)! I meant to post this line...
> >>
> >> bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
> >>
> >> But in looking at it now, I see we can shorten it even more...
> >>
> >> bCh = sStr Like "[!0]#" Or sStr Like "0[5-9]"
> >
> > That will allow things like "a0" to pass as True
>
> Damn those last minute inspirations! Yep, you are right, [!0] is
definitely
> not the way to go.
>
> Elena
> =====
> This one (the first way) still works though....
>
> bCh = sStr Like "[1-9]#" Or sStr Like "0[5-9]"
>
> Rick
>
> |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Thu Jan 04, 2007 5:16 pm Post subject: Re: Help with "Like" operator |
|
|
> LOL!
>
> I reckon you guys have had a bit too much of the
> Christmas (& New Year) spirit
You might think so given my obfuscated one-liner "challenge" posting.
Rick
|
|
| Back to top |
|
 |
|