469,312 Members | 2,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Trap Input

nkp
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.

Jul 17 '05 #1
10 4173
> Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.


When the user clicks on your OK, Next, Continue (or whatever caption you
have) button, check the contents of the TextBox to see if it has the "shape"
you want. If not, pop up a warning dialog box and, after the user clicks OK
on it, take them back to the "offending" TextBox so they can correct their
entry.

Rick - MVP
Jul 17 '05 #2

"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.


1.
YOUR SUB

IF VAL(TRIM(Text1.Text)) >= 0 THEN
'handle valid numerical entry
ELSE
'let user know, e.g
MSGBOX("non-negative numbers only please")
END IF

IF LEN(TRIM(Text1.Text)) > 7 THEN
' valid user password
ELSE
' let user know. Might need a counter here
' like if it exceed 3 to quit the program or
' something like that
MSGBOX("Incorrect password. try again.")
END IF


Jul 17 '05 #3
'Positive numbers only:
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Often, to ensure that users enter only numbers in a text field, you'll want
to validate the text as they enter it. The textbox's Change() event provides
the best place to do so. However, simply using the IsNumeric() function
alone won't do the trick. For instance, suppose the user entered a negative
number. The IsNumeric() function simply wont see it and will return an
error. There is a way around this.
Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.

Jul 17 '05 #4
nkp
Thanks Raoul....just what I needed!
Raoul Watson wrote:
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.

1.
YOUR SUB

IF VAL(TRIM(Text1.Text)) >= 0 THEN
'handle valid numerical entry
ELSE
'let user know, e.g
MSGBOX("non-negative numbers only please")
END IF

IF LEN(TRIM(Text1.Text)) > 7 THEN
' valid user password
ELSE
' let user know. Might need a counter here
' like if it exceed 3 to quit the program or
' something like that
MSGBOX("Incorrect password. try again.")
END IF


Jul 17 '05 #5
If Len(Text1.Text) < 8 then
MsgBox ("Must be at least 8 characters")
Text1.Text = ""
End If
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.

Jul 17 '05 #6
I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

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

"MooVBuff" <ed*****@comcast.net> wrote in message
news:zu********************@comcast.com...
'Positive numbers only:
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Often, to ensure that users enter only numbers in a text field, you'll want to validate the text as they enter it. The textbox's Change() event provides the best place to do so. However, simply using the IsNumeric() function
alone won't do the trick. For instance, suppose the user entered a negative number. The IsNumeric() function simply wont see it and will return an
error. There is a way around this.
Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.


Jul 17 '05 #7
Well put. Thanks.

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:8-********************@comcast.com...
I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

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

"MooVBuff" <ed*****@comcast.net> wrote in message
news:zu********************@comcast.com...
'Positive numbers only:
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Often, to ensure that users enter only numbers in a text field, you'll

want
to validate the text as they enter it. The textbox's Change() event

provides
the best place to do so. However, simply using the IsNumeric() function
alone won't do the trick. For instance, suppose the user entered a

negative
number. The IsNumeric() function simply wont see it and will return an
error. There is a way around this.
Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box( where a password must be a minimum of 8 characters for example)
Thank you.



Jul 17 '05 #8
Hope you don't mind a slight digression, and an invitation to comment.

I have found that users expect a number as displayed to also be acceptable as
input. For instance, if a number is displayed as $1,254 and the use edits it to
read $1,243 they expect this edited version to be accepted. This is particularly
a problem for me because our apps tend to have a lot of feet inch, and angle
measurements, and display things like 49.75" or 37.75' or 15.23 - or even N 27
13' 42" E.

While less elegant that the use of Like (which I hadn't used in this kind of
situation, thanks for the idea), I have usually gone in the direction of
stripping the input text to the characters "0123456789-.", like this:

strTemp = ""
nCnt = Len(InputString)

For n = 1 To nCnt
strChar = Mid(InputString, n, 1)
If InStr(1, "0123456789-.", strChar, vbBinaryCompare) Then
strTemp = strTemp & strChar
End If
Next n

Then either returning true if the result is numeric (i.e. IsNumeric(strTemp)),
or more often, applying the appropriate format, whether currency, feet, inches,
angle, or whatever, and displaying that.

By the way, rather than msgboxing the user, I usually just restore the previous
value if the user's new entry is no good.

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:8-********************@comcast.com...
I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

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

"MooVBuff" <ed*****@comcast.net> wrote in message
news:zu********************@comcast.com...
'Positive numbers only:
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Often, to ensure that users enter only numbers in a text field, you'll

want
to validate the text as they enter it. The textbox's Change() event

provides
the best place to do so. However, simply using the IsNumeric() function
alone won't do the trick. For instance, suppose the user entered a

negative
number. The IsNumeric() function simply wont see it and will return an
error. There is a way around this.
Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
"nkp" <we***********@btinternet.com> wrote in message
news:bm**********@sparta.btinternet.com...
Sorry guys, basic questions.....
1)how do you prevent negative numeric input in a text box (-2345 etc)
2)how do detect /prevent input of LESS than 8 characters in a text box(
where a password must be a minimum of 8 characters for example)
Thank you.



Jul 17 '05 #9
> I have found that users expect a number as displayed to also be acceptable
as
input. For instance, if a number is displayed as $1,254 and the use edits it to read $1,243 they expect this edited version to be accepted. This is particularly a problem for me because our apps tend to have a lot of feet inch, and angle measurements, and display things like 49.75" or 37.75' or 15.23 - or even N 27 13' 42" E.
Usually what I do for things like currency or dimensioned items (not your
last direction example though), is to display it with the proper formating,
but remove the special characters and display only the numbers when they
click into the TextBox (using its GotFocus event) to edit it. Then, when the
TextBox loses focus, I check the number and display it with proper
fomatting, if valid, or indicate it is not valid in some way otherwise.

While less elegant that the use of Like (which I hadn't used in this kind of situation, thanks for the idea), I have usually gone in the direction of
stripping the input text to the characters "0123456789-.", like this:

strTemp = ""
nCnt = Len(InputString)

For n = 1 To nCnt
strChar = Mid(InputString, n, 1)
If InStr(1, "0123456789-.", strChar, vbBinaryCompare) Then
strTemp = strTemp & strChar
End If
Next n

Then either returning true if the result is numeric (i.e. IsNumeric(strTemp)), or more often, applying the appropriate format, whether currency, feet, inches, angle, or whatever, and displaying that.
Wouldn't this allow them to type in complete garbage (perhaps an assumed
value in the clipboard that was not actually copied into there) and have
your program accept it as if it's valid? For example, if I entered this into
the TextBox, "H1EL2LO3", your program would assume I meant 123 and process
it.

By the way, rather than msgboxing the user, I usually just restore the previous value if the user's new entry is no good.


You don't give them a signal that their entry was wrong? An argument can be
made for leaving the input as the user originally typed it... it makes it
easier for them to correct maybe one bad or incorrect character than type
the entire entry over again.
Rick - MVP
Jul 17 '05 #10
Thanks for the comments, Rick. I made a few comments in line, and I may pursue
this more at work, when I get time...

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:l_********************@comcast.com...
I have found that users expect a number as displayed to also be acceptable as
input. For instance, if a number is displayed as $1,254 and the use edits

it to
read $1,243 they expect this edited version to be accepted. This is

particularly
a problem for me because our apps tend to have a lot of feet inch, and

angle
measurements, and display things like 49.75" or 37.75' or 15.23 - or even

N 27
13' 42" E.


Usually what I do for things like currency or dimensioned items (not your
last direction example though), is to display it with the proper formating,
but remove the special characters and display only the numbers when they
click into the TextBox (using its GotFocus event) to edit it. Then, when the
TextBox loses focus, I check the number and display it with proper
fomatting, if valid, or indicate it is not valid in some way otherwise.


This would work, I think, except for the angles and bearings in degrees,
minutes, seconds. Those are their own kind of pain in the neck. It would
also let them see a few more decimal places in certain situations, which
would help, as long as its not too many decimal places.
While less elegant that the use of Like (which I hadn't used in this kind of
situation, thanks for the idea), I have usually gone in the direction of
stripping the input text to the characters "0123456789-.", like this:

strTemp = ""
nCnt = Len(InputString)

For n = 1 To nCnt
strChar = Mid(InputString, n, 1)
If InStr(1, "0123456789-.", strChar, vbBinaryCompare) Then
strTemp = strTemp & strChar
End If
Next n

Then either returning true if the result is numeric (i.e.

IsNumeric(strTemp)),
or more often, applying the appropriate format, whether currency, feet,

inches,
angle, or whatever, and displaying that.


Wouldn't this allow them to type in complete garbage (perhaps an assumed
value in the clipboard that was not actually copied into there) and have
your program accept it as if it's valid? For example, if I entered this into
the TextBox, "H1EL2LO3", your program would assume I meant 123 and process
it.


Yes it would. Fortunately, they mostly type in almost correct things, making
your next comment more important. Also, they copy and paste formatted
numbers from one text box to another, although I think the GotFocus
treatment you mentioned above would prevent them copying and pasting
formatted numbers.
By the way, rather than msgboxing the user, I usually just restore the previous
value if the user's new entry is no good.


You don't give them a signal that their entry was wrong? An argument can be
made for leaving the input as the user originally typed it... it makes it
easier for them to correct maybe one bad or incorrect character than type
the entire entry over again.


True enough. Mostly the numbers are short enough that its not much of a
problem to re-enter them, and they seem to notice right away if an entry
didn't take. I am reluctant to actually trap them in the textbox until its
fixed, because they made need to view some other thing first. Maybe I can
leave the text they entered, make it red or something, and not save until it
gets fixed.

Rick - MVP


Steve
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rebecca Smith | last post: by
11 posts views Thread by pemo | last post: by
10 posts views Thread by pemo | last post: by
3 posts views Thread by Robert W. | last post: by
3 posts views Thread by SalP | last post: by
6 posts views Thread by temper3243 | last post: by
2 posts views Thread by Linda Liu[MSFT] | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.