msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with "Like" operator

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
elena



Joined: 04 Oct 2007
Posts: 9

PostPosted: Tue Jan 02, 2007 6:34 pm    Post subject: Help with "Like" operator Reply with quote

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
View user's profile Send private message
Ken Halter



Joined: 04 Oct 2007
Posts: 4150

PostPosted: Tue Jan 02, 2007 7:16 pm    Post subject: Re: Help with "Like" operator Reply with quote

"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
View user's profile Send private message
Ken Halter



Joined: 04 Oct 2007
Posts: 4150

PostPosted: Tue Jan 02, 2007 7:21 pm    Post subject: Re: Help with "Like" operator Reply with quote

"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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Wed Jan 03, 2007 12:51 am    Post subject: Re: Help with "Like" operator Reply with quote

> 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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Wed Jan 03, 2007 1:03 am    Post subject: Re: Help with "Like" operator Reply with quote

>> 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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Wed Jan 03, 2007 1:09 am    Post subject: Re: Help with "Like" operator Reply with quote

>>> 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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Wed Jan 03, 2007 7:42 am    Post subject: Re: Help with "Like" operator Reply with quote

> 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
View user's profile Send private message
Bob Butler



Joined: 04 Oct 2007
Posts: 1325

PostPosted: Wed Jan 03, 2007 10:42 am    Post subject: Re: Help with "Like" operator Reply with quote

"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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Wed Jan 03, 2007 2:43 pm    Post subject: Re: Help with "Like" operator Reply with quote

>> 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
View user's profile Send private message
Tony Proctor



Joined: 04 Oct 2007
Posts: 1051

PostPosted: Thu Jan 04, 2007 3:27 pm    Post subject: Re: Help with "Like" operator Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Thu Jan 04, 2007 5:16 pm    Post subject: Re: Help with "Like" operator Reply with quote

> 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
View user's profile Send private message
Display posts from previous:   
Related Topics:
[] operator what is [] operator? for example: Thanks for help -- tomm

Operator Overloading Does VB.NET support overloaded operators? For instance, if I create a complex number class, Complex, can I do code like this? Dim a As New Complex(1.2, 3.4) Dim b As New Complex(5.6, 7.89) b = b * 4 a = a + b I'm not sure if Operator Overloading is an OO

VB Equivalent of Right Shift operator ( >> ) What is a VB6 equivalent of a Right Shift operator (>>)? I am trying to convert a C++ code, into visual basic, that looks like the following: While (m < n) { k = (m+n) >> 1 } Thanks, Bob

string concatentation using & or + operator? I want to know for string in VB6, are we supposed to use & operator?? In the following example, both are working fine, and both s1 and s2 will be "hello world" Dim s1 As String Dim s2 As String s1 = "hello " + "world" s2 = "hello " & "worl

When should I use OR operator and when '+' ? Hello, I think using OR operator is preferrable to using '+', but isn't it the same? For example: Const SWP_NOMOVE = &H2 Const SWP_NOSIZE = &H1 Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE when I do: Const TOPMOST_FLAGS = SWP_NOMOVE + SWP_NOSIZE the res
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group