473,804 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4424
> 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.btint ernet.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("Incorre ct 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(strTe xt))
End Function
"nkp" <we***********@ btinternet.com> wrote in message
news:bm******** **@sparta.btint ernet.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.btint ernet.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("Incorre ct 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.btint ernet.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,,E6 7$)")

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(Va lue 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*****@comcas t.net> wrote in message
news:zu******** ************@co mcast.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(strTe xt))
End Function
"nkp" <we***********@ btinternet.com> wrote in message
news:bm******** **@sparta.btint ernet.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.c om...
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,,E6 7$)")

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(Va lue 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*****@comcas t.net> wrote in message
news:zu******** ************@co mcast.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(strTe xt))
End Function
"nkp" <we***********@ btinternet.com> wrote in message
news:bm******** **@sparta.btint ernet.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(strTe mp)),
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.c om...
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,,E6 7$)")

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(Va lue 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*****@comcas t.net> wrote in message
news:zu******** ************@co mcast.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(strTe xt))
End Function
"nkp" <we***********@ btinternet.com> wrote in message
news:bm******** **@sparta.btint ernet.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(strTe mp)), 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1772
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. Thank. Rebecca
2
395
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 plain html, not with asp.net how can i do this ?
11
437
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 the stored value of an object has such a representation and is read by an lvalue
10
2366
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 sure what 'a trap' means here - anyone?). I also read into this that, an *initialised* automatic variable, may never hold a bit pattern that might, when read, cause a 'trap'. I.e., if an auto is explicitly initialised, it may *never* hold a...
3
5015
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 generic event handler: private void textBoxMakeReadOnly(object sender, KeyPressEventArgs e) { e.Handled = true; }
3
1146
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 "This page cannot be displayed screen". I also tried adding a Page Error handler which didn't work. I want to tell the user that the file size is too large. Thanks, Sal
6
2514
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
3325
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 KeyDown event is fired, but not when I press a direction key. I want to see them in the KeyDown event so that I can respond as soon as the key is depressed, and I want to detect when the key is held down so that I can perform an action...
2
6534
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 the focus by clicking the mouse or tapping the stylus on the control in question, or by using navigation keys such as the Tab and cursor keys. In principle, any user interface element can receive the focus. The IsFocused property is defined on...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.