469,356 Members | 2,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Ranges

dfg
Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.

I was thinking of using a Case statement to compare if it is within the
proper range. I just can't seem to figure out how to check to see if it
is outside the range. An array perhaps?

Any thoughts or pointers?

Thanks.

Jul 17 '05 #1
18 3456
dfg wrote:
Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.

I was thinking of using a Case statement to compare if it is within the
proper range. I just can't seem to figure out how to check to see if it
is outside the range. An array perhaps?

Any thoughts or pointers?

Thanks.


You can test with the Case Statements - simply include a 'Case Else'
statement, and use that for your 'Out of Range' routine.

Scott C. Zielinski

Jul 17 '05 #2

"Scott Zielinski" <ke********@comcast.net> wrote in message
news:Fs********************@comcast.com...
dfg wrote:
Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.

I was thinking of using a Case statement to compare if it is within the
proper range. I just can't seem to figure out how to check to see if it
is outside the range. An array perhaps?

Any thoughts or pointers?

Thanks.


You can test with the Case Statements - simply include a 'Case Else'
statement, and use that for your 'Out of Range' routine.

Scott C. Zielinski


If your entry is numbers all you have to do is use "If" to compare it: "If
MyNumber < 5 Then..." it is outside the range.

A text box entry might need to be first converted to numeric entry for you
to test it. That can be done with "Val" by assigning to a numeric variable
with "=Val( [String Var])". For example:-

Dim MyNumber as Integer

MyNumber = Val(TextBox1.Text) 'Assign contents of box to integer variable

If MyNumber < 5 Then
MsgBox("Sorry that number was too high! Please try again.")
End If

A useful thing is "IsNumeric" for testing whether the contents of a variable
are numeric. (A variant variable takes either a number or a string). "If
IsNumeric(MyNumber) Then..."

I am not positive about it but one way this kind of thing might also be done
is by using the "Like Operator". This enables you to set a pattern and match
the user's entry against the pattern: see Help for "Like Operator" and its
syntax. I know it copes with ranges of letters in the alphabet. I suspect it
would work because to the Like Operator numbers are characters.

Just ideas.
Mark Mulvany




Jul 17 '05 #3
Mark Mulvany wrote:
dfg wrote:
> [...] I don't want to convert them to numbers. [...]
>
<snip>
If your entry is numbers all you have to do is use "If" to compare it:
"If MyNumber < 5 Then..." it is outside the range.

A text box entry might need to be first converted to numeric entry for you
to test it.


He specifically says that he /doesn't/ want to convert to numbers.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Jul 17 '05 #4
On Sun, 23 Nov 2003 03:10:40 GMT, dfg <df*@dfg.net> wrote:
Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.

I was thinking of using a Case statement to compare if it is within the
proper range. I just can't seem to figure out how to check to see if it
is outside the range. An array perhaps?

Any thoughts or pointers?


If InStr( "12345", N$ ) Then ...
Jul 17 '05 #5
"Mark Mulvany" <mu*****@melbpc.org.au> wrote in
news:bp**********@possum.melbpc.org.au:

"Scott Zielinski" <ke********@comcast.net> wrote in message
news:Fs********************@comcast.com...
dfg wrote:
> Hello. I am trying to figure out how I can find out if a character
> field is within a certain range. For example, I have a textbox and
> I would like to check to see if the data entered is between 01 to
> 05. I don't want to convert them to numbers. I need the leading
> zero.
>
> I was thinking of using a Case statement to compare if it is within
> the proper range. I just can't seem to figure out how to check to
> see if it is outside the range. An array perhaps?
>
> Any thoughts or pointers?


A simple solution is to create a function taking your input string as a
parameter and returning a boolean (true or false) value.

An example (in Ada) of such a function would be:

function In_Range (Item : String) return Boolean is
subtype Small_String is String(1..2);
type Value_List is array(1..5) of Small_String;
Good_Values : constant Value_List := ("01", "02",
"03", "04",
"05");
Result : Boolean := False;
begin
for Index in Good_Values'Range loop
if Item = Good_Values(Index) then
Result := True;
end if;
end loop;
return Result;
end In_Range;

When performing the test for the value from the text box simply call
the function:

if In_Range(TextBox_Value) then
-- Do whatever you should do
else
-- Handle out of range condition
end if;

Jim Rogers
Jul 17 '05 #6
> Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.


In other words, your user **must** type in his/her entry **with** the
leading zero in order for it to be acceptable to you (that is, a user entry
of 3 would be unacceptable whereas an entry of 03 would be fine), right?

If Text1.Text >= "01" And Text1.Text <= "05" Then
MsgBox "Your entry is OK"
Else
MsgBox "The entry is invalid!"
End If

Rick - MVP
Jul 17 '05 #7
> > Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and I
would like to check to see if the data entered is between 01 to 05. I
don't want to convert them to numbers. I need the leading zero.
In other words, your user **must** type in his/her entry **with** the
leading zero in order for it to be acceptable to you (that is, a user

entry of 3 would be unacceptable whereas an entry of 03 would be fine), right?

If Text1.Text >= "01" And Text1.Text <= "05" Then
MsgBox "Your entry is OK"
Else
MsgBox "The entry is invalid!"
End If


Mark raised the idea of using the Like operator. This is how you would do
the above using it instead of testing the range as I did above...

If Text1.Text Like "0[1-5]" Then
MsgBox "Your entry is OK"
Else
MsgBox "The entry is invalid!"
End If
Rick - MVP
Jul 17 '05 #8
> A useful thing is "IsNumeric" for testing whether the contents of a
variable
are numeric. (A variant variable takes either a number or a string). "If
IsNumeric(MyNumber) Then..."


IsNumeric is not nearly as useful for parsing numbers as you are indicating.
Here is a posting that I have offered in the past on this function. (The
alternate functions at the end don't really relate to the question at hand,
but I've left them in case anyone who hasn't seen my posting before needs
the kind of functionality they provide.)

Rick - MVP

I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post for
non-US regional settings):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP
Jul 17 '05 #9
Rick ...

I've read this response a number of times, and understand the message you
attempting to present, however I believe the example

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

.... and the statement "Most people would not expect THAT to return True." is
not quite correct. Your statement assumes the developer viewing the
complexity and formatting of the expression would expect a False result.

One of the MSDN definitions for IsNumeric indicates it will return True if
the value passed is a string _and_ that string can be successfully converted
to a double. This is the case for the above ...

Print CDbl("($1,23,,3.4,,,5,,E67$)")

Thus, given the MSDN inputbox entry example:

Do
anyNumber = InputBox("Enter a number")
Loop Until IsNumeric(anyNumber)

.... the loop would properly terminate when the string above was entered.

While your example may be representative of what may, upon first
examination, be perceived as a string that should fail the IsNumeric test, I
believe the MSDN example of using :

ReturnValue = IsNumeric("45 some text here")

remains a better (and clearer) example showing the possibility of misusing
IsNumeric against a string containing a numeric value where the user may
interpret that any variable that passes a Val() test should pass an
IsNumeric() test.

My intention is not to represent IsNumeric as being a better test than any
other available methods for determining whether a value is a valid number,
just that the complexity of a valid string does not negate the benefit of
using IsNumeric as judiciously as any other function would be used.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
Jul 17 '05 #10
See inline comments...
I've read this response a number of times, and understand the message you
attempting to present, however I believe the example

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

... and the statement "Most people would not expect THAT to return True." is not quite correct. Your statement assumes the developer viewing the
complexity and formatting of the expression would expect a False result.
Actually, I think my statement is correct... I would be willing to bet money
that most (much more than 50%) of programmers (not already intimately
familiar with IsNumeric's "quirks") would expect that string value to return
a False value. Obviously, the "numeric" string I show is an **extreme**
example of what IsNumeric will verify as being a "numeric value" and is
provided for its *shock* value; however, it has been my experience that
almost all posters looking to verify numeric input are looking for either
all digits or for a properly formatted floating point value (hence, the two
functions I include with the post). IsNumeric is not a reliable function to
use to check for those types of input. I tried to cover the justification
for that statement in the rest of my posted message. If a function suggests
by its name that it will judge as True only (what people tend to think are)
"pure" numbers, then it should reject those possible mistyping which are
"near" numeric in nature... IsNumeric does not meet that criteria (in my
opinion).

One of the MSDN definitions for IsNumeric indicates it will return True if
the value passed is a string _and_ that string can be successfully converted to a double. This is the case for the above ...

Print CDbl("($1,23,,3.4,,,5,,E67$)")

Thus, given the MSDN inputbox entry example:

Do
anyNumber = InputBox("Enter a number")
Loop Until IsNumeric(anyNumber)

... the loop would properly terminate when the string above was entered.
I understand that is what Microsoft thinks IsNumeric should do; it's just
that I don't think everyday programmers will agree. Really now, a currency
symbol at **both** ends of the "number" or a plus/minus sign at the end?

While your example may be representative of what may, upon first
examination, be perceived as a string that should fail the IsNumeric test, I believe the MSDN example of using :

ReturnValue = IsNumeric("45 some text here")

remains a better (and clearer) example showing the possibility of misusing
IsNumeric against a string containing a numeric value where the user may
interpret that any variable that passes a Val() test should pass an
IsNumeric() test.
Maybe... although IsNumeric will return True for "$12" and Val will return
0. Plus I think the fact that Val returns the leading numeric part of a
string composed of digits and non-digits (zero if the lead character is not
a digit) is kind of "quirky" if you ask me. I think maybe "12d34" and
"12d345" might be a better way to show (one of) the problems with IsNumeric
(the first is True and the second is False).

My intention is not to represent IsNumeric as being a better test than any
other available methods for determining whether a value is a valid number,
just that the complexity of a valid string does not negate the benefit of
using IsNumeric as judiciously as any other function would be used.


I think I'll stand by my original posting in response to your statement.<g>
The only time I find where IsNumeric **might** be of any possible use is in
checking whether a single character is a digit or not. Other than that, I
avoid it like the plague and would recommend others to do the same... it's
just too easy to construct a test that does what you want without having to
put up with the generalized quirks in IsNumeric.
Rick - MVP
Jul 17 '05 #11
> ..... (hence, the two functions I include with the post)...

Those two functions were offered for simplicity sake; but they were
regionally biased to the US settings. Here is a function, extended to allow
thousands separators to be entered by the user (although no check is made as
to their proper placement) which combines the functionality of both and
removes the regional settings bias. The framework structure is there so that
the reader can modify the function to allow for other things, like a
currency symbol, if desired. I know you would be happier with deriving the
regional decimal point and thousands separator via some API calls; but, hey,
this is me...<g>

Rick - MVP

Function IsNumber(ByVal Value As String, _
Optional DigitsOnly As Boolean = True, _
Optional AllowSign As Boolean = False, _
Optional AllowSeparator As Boolean = False) _
As Boolean

Dim DP As String
Dim TS As String
If AllowSign Then
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
End If
If AllowSeparator Then
' Get local setting for thousands'
' separator and eliminate them.
TS = Mid$(Format$(1000, "#,###"), 2, 1)
Value = Replace$(Value, TS, "")
End If
If DigitsOnly Then
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9]*"
Else
' Get local setting for decimal point
DP = Format$(0, ".")
IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) > 0 And Value <> DP
End If

End Function
Jul 17 '05 #12

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:md********************@comcast.com...
See inline comments...

I think I'll stand by my original posting in response to your statement.<g> The only time I find where IsNumeric **might** be of any possible use is in checking whether a single character is a digit or not. Other than that, I avoid it like the plague and would recommend others to do the same... it's just too easy to construct a test that does what you want without having to put up with the generalized quirks in IsNumeric.
Rick - MVP


I use IsNumeric as a pre-condition for conversions using CDbl() and
CLng(). My experience is that the conversion will not generate an error
if the argument passes the IsNumeric test. In this sense, it is similar
to IsDate() used with CDate().

I think Randy's post points out that this is MS's intent for
IsNumeric - it is a conversion checking measure, not an input validation
tool.

Your post is invaluable, however, since so many programmers (mea culpa)
have mistakenly or carelessly used IsNumeric as a primary input
vailidation test.
Jul 17 '05 #13
dfg
James Rogers wrote:
"Mark Mulvany" <mu*****@melbpc.org.au> wrote in
news:bp**********@possum.melbpc.org.au:

"Scott Zielinski" <ke********@comcast.net> wrote in message
news:Fs********************@comcast.com...
dfg wrote:

Hello. I am trying to figure out how I can find out if a character
field is within a certain range. For example, I have a textbox and
I would like to check to see if the data entered is between 01 to
05. I don't want to convert them to numbers. I need the leading
zero.

I was thinking of using a Case statement to compare if it is within
the proper range. I just can't seem to figure out how to check to
see if it is outside the range. An array perhaps?

Any thoughts or pointers?

A simple solution is to create a function taking your input string as a
parameter and returning a boolean (true or false) value.

An example (in Ada) of such a function would be:

function In_Range (Item : String) return Boolean is
subtype Small_String is String(1..2);
type Value_List is array(1..5) of Small_String;
Good_Values : constant Value_List := ("01", "02",
"03", "04",
"05");
Result : Boolean := False;
begin
for Index in Good_Values'Range loop
if Item = Good_Values(Index) then
Result := True;
end if;
end loop;
return Result;
end In_Range;

When performing the test for the value from the text box simply call
the function:

if In_Range(TextBox_Value) then
-- Do whatever you should do
else
-- Handle out of range condition
end if;

Jim Rogers


That's what I was looking for...Thanks. I haven't used Ada in a few
years, but that brings back some memories. :-)

Jul 17 '05 #14
dfg
dfg wrote:

Any thoughts or pointers?

Thanks.


To Mark, Rick et al. Thanks everyone, it worked out great...

--
SPAM is good with crackers, not email.

Jul 17 '05 #15
> I use IsNumeric as a pre-condition for conversions using CDbl() and
CLng(). My experience is that the conversion will not generate an error
if the argument passes the IsNumeric test. In this sense, it is similar
to IsDate() used with CDate().
Yes, you are right here... of course.

I think Randy's post points out that this is MS's intent for
IsNumeric - it is a conversion checking measure, not an input validation
tool.
Yes, I think I read right over that point in his post; it's not how most
people use IsNumeric (as you point out below). However, given that
functionality, what possible rationalization is ther for IsNumeric (and the
CXXX functions) accepting some of the things it does? Why leading and
trailing currency symbols at the same time? Why trailing plus/minus signs?

Your post is invaluable, however, since so many programmers (mea culpa)
have mistakenly or carelessly used IsNumeric as a primary input
vailidation test.


Thanks.
Rick - MVP
Jul 17 '05 #16
In article <bp**********@titan.btinternet.com>, bi****@eton.powernet.co.uk wrote:
Mark Mulvany wrote:
dfg wrote:
> [...] I don't want to convert them to numbers. [...]
>

<snip>

If your entry is numbers all you have to do is use "If" to compare it:
"If MyNumber < 5 Then..." it is outside the range.

A text box entry might need to be first converted to numeric entry for you
to test it.


He specifically says that he /doesn't/ want to convert to numbers.


He can convert a copy to a number, though!

- Gerry Quinn
Jul 17 '05 #17

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:Vr********************@comcast.com...

Yes, I think I read right over that point in his post; it's not how most people use IsNumeric (as you point out below). However, given that
functionality, what possible rationalization is ther for IsNumeric (and the CXXX functions) accepting some of the things it does? Why leading and
trailing currency symbols at the same time? Why trailing plus/minus signs?


This may be a stretch, but I think that individually, the various items
could occur in a valid number. I think there are some countries where
the currency sign comes after the number. Some accountants do that
parentheses thing for negatives, so maybe some do the trailing minus
sign. The function seems to have been built so that any one of these
could be allowed to slip through, which is arguably reasonable, but it
also allows any mixture of them, which clearly is not. I guess it was
not really thought through until it was too late.
Jul 17 '05 #18
Rick Rothstein wrote:
Actually, I think my statement is correct... I would be willing to
bet money that most (much more than 50%) of programmers (not already
intimately familiar with IsNumeric's "quirks") would expect that
string value to return a False value.


Can't address the >50% aspect, but just as a data point, I'm aware
of IsNumeric's problems (I knew the example would return True), and
I consider the function broken because of the behavior in question.

--
|_ CJSonnack <Ch***@Sonnack.com> _____________| How's my programming? |
|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL |
|_____________________________________________|___ ____________________|
Jul 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Alf P. Steinbach | last post: by
12 posts views Thread by Steve Elliott | last post: by
5 posts views Thread by John Brock | last post: by
6 posts views Thread by Robin Haswell | last post: by
67 posts views Thread by PC Datasheet | last post: by
9 posts views Thread by Christoph Bartoschek | last post: by
3 posts views Thread by Snedker | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.