473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4388
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rebecca Smith | last post by:
I'd like to trap an error if a user inputs a bogus time. The input requires a short time format. If the user enters someting like 20:78, Access will catch that but I'd like something of my own....
2
by: Rocio | last post by:
I have a html button created with <input type="submit" id="btnPayNotices" value="Pay Notices" /> now I need to trap the click event at the server side. Yes, this button had to be created with...
11
by: pemo | last post by:
Ambiguous? I have a student who's asked me to explain the following std text (esp. the footnote). 6.2.6.1.5 Certain object representations need not represent a value of the object type. If...
10
by: pemo | last post by:
As far as I understand it, a trap representation means something like - an uninitialised automatic variable might /implicitly/ hold a bit-pattern that, if read, *might* cause a 'trap' (I'm not...
3
by: Robert W. | last post by:
In my WinForms app I wanted to implement a Read-Only textbox. I didn't like the appearance a textbox takes on when the ReadOnly property is set true so instead I trapped the KeyPress event with a...
3
by: SalP | last post by:
I'm using VS 2003. I'm uplading a file using Input Type='File' .PostedFile. etc. How does one trap the error when the file is larger than the 4 Meg default? Try/Catch doesn't work. I get the...
6
by: temper3243 | last post by:
Hi Can someone explain me what is happening below ? Why is it printing 401380 $ cat scanf.c #include<stdio.h> int main() { int i; scanf(" %d",&i);
2
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's...
2
by: Linda Liu[MSFT] | last post by:
Hi George, Keyboard input introduces the idea of focus. In Windows, a particular element is designated as having the focus, meaning that it acts as the target for keyboard input. The user sets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.