473,769 Members | 3,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

viusal basic input validation integer

Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?

isNumeric() checks for a numeric value .. but does not notify of numbers
with decimal places
inputBox returns a string so I could check for decimal point??? this seems
like overkill

The value returned can be asigned into an Integer type and then a Single
type ... the two can be compared .. but still this does not complain about
numbers with decimal places if the fractional part is zero

what is the simple solution?

cw
Feb 28 '06 #1
27 35947
code_wrong wrote:
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?


Use a masked input box that rejects invalid entries at the keystroke.

Look up "masked input" - there were lots of options for VB6. 6 years ago.

And upgrade to a dynamic OO language as soon as you can. VB6 will bring you
down.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 28 '06 #2

"Phlip" <ph*******@gmai l.com> wrote in message
news:ia******** **********@news svr30.news.prod igy.com...
code_wrong wrote:
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?


Use a masked input box that rejects invalid entries at the keystroke.

Look up "masked input" - there were lots of options for VB6. 6 years ago.

And upgrade to a dynamic OO language as soon as you can. VB6 will bring
you
down.


unfortunately I am restricted to using inputbox() ... why? .. if I change
the method for input now, the students will likely will be traumatised ..
these are very early days
Feb 28 '06 #3
> Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?


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 = Len(Value) > 0 And _
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

Here are revisions to the above functions that deal with the local settings
for decimal points (and thousand's separators) that are different than used
in the US (this code works in the US too, of course).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
' Get local setting for decimal point
DP = Format$(0, ".")
' 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" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) > 0 And Value <> DP And _
Value <> vbNullString
End Function

I'm not as concerned by the rejection of entries that include one or more
thousand's separators, but we can handle this if we don't insist on the
thousand's separator being located in the correct positions (in other words,
we'll allow the user to include them for their own purposes... we'll just
tolerate their presence).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
Dim TS As String
' Get local setting for decimal point
DP = Format$(0, ".")
' Get local setting for thousand's separator
' and eliminate them. Remove the next two lines
' if you don't want your users being able to
' type in the thousands separator at all.
TS = Mid$(Format$(10 00, "#,###"), 2, 1)
Value = Replace$(Value, TS, "")
' 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" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) > 0 And Value <> DP And _
Value <> vbNullString
End Function

Rick
Feb 28 '06 #4
code_wrong wrote:
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?

isNumeric() checks for a numeric value .. but does not notify of numbers
with decimal places
inputBox returns a string so I could check for decimal point??? this seems
like overkill

The value returned can be asigned into an Integer type and then a Single
type ... the two can be compared .. but still this does not complain about
numbers with decimal places if the fractional part is zero

what is the simple solution?

cw


What you can do on a text box control that is being used for only
numbers is use the Keypress Event and check the key being pressed. If
it's a numeric key 0-9, you accept the keypress. If the key is not a
numeric key 0-9, the you cancel or don't accept the keypress. I recall
ASCII code 0 or something like that cancels key-code from a key press so
that it's not accepted.

You just write a Keypress event routine that accepts numeric keys or
possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
focus to the next control), take the appropriate actions on the
control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.

Duane :)
Feb 28 '06 #5
> What you can do on a text box control that is being used for only
numbers is use the Keypress Event and check the key being pressed. If
it's a numeric key 0-9, you accept the keypress. If the key is not a
numeric key 0-9, the you cancel or don't accept the keypress. I recall
ASCII code 0 or something like that cancels key-code from a key press so
that it's not accepted.

You just write a Keypress event routine that accepts numeric keys or
possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
focus to the next control), take the appropriate actions on the
control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.


You can't take appropriate action on "true" control keys in the KeyPress
event; that requires the KeyUp or KeyDown event. And if you don't handle all
of the keystrokes that allow a user to Paste data (and the Mouse events that
allow it too), then your user will be able to Paste non-digits into the
TextBox. And, after all of that, I think you will still have missed one or
two places where the user can initiate a Paste operation.

Rick
Feb 28 '06 #6
Rick Rothstein [MVP - Visual Basic] wrote:
What you can do on a text box control that is being used for only
numbers is use the Keypress Event and check the key being pressed. If
it's a numeric key 0-9, you accept the keypress. If the key is not a
numeric key 0-9, the you cancel or don't accept the keypress. I recall
ASCII code 0 or something like that cancels key-code from a key press so
that it's not accepted.

You just write a Keypress event routine that accepts numeric keys or
possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
focus to the next control), take the appropriate actions on the
control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.

You can't take appropriate action on "true" control keys in the KeyPress
event; that requires the KeyUp or KeyDown event. And if you don't handle all
of the keystrokes that allow a user to Paste data (and the Mouse events that
allow it too), then your user will be able to Paste non-digits into the
TextBox. And, after all of that, I think you will still have missed one or
two places where the user can initiate a Paste operation.

Rick


You may be right I don't recall. However, I have used Keyup, Keydown,
Keypress and Mouse events. In the real world or in accounting
appliactions in business solutions in corporations where I have applied
the numeric only solution, it's worked like a champ on pure data entry
applications where the numbers had to be banged in - there is no cutting
and pasting.

It's something the poster is going to have to figure out for his or
herself as to the best approach.

Duane :)

Mar 1 '06 #7
Rick ...

Is not: Not Value Like "*[!0-9]*" ... redundant?

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.


"Rick Rothstein [MVP - Visual Basic]" <ri************ @NOSPAMcomcast. net>
wrote in message news:-c************** *************** *@comcast.com.. .
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?


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 = Len(Value) > 0 And _
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

Here are revisions to the above functions that deal with the local settings
for decimal points (and thousand's separators) that are different than used
in the US (this code works in the US too, of course).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
' Get local setting for decimal point
DP = Format$(0, ".")
' 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" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) > 0 And Value <> DP And _
Value <> vbNullString
End Function

I'm not as concerned by the rejection of entries that include one or more
thousand's separators, but we can handle this if we don't insist on the
thousand's separator being located in the correct positions (in other words,
we'll allow the user to include them for their own purposes... we'll just
tolerate their presence).

Function IsNumber(ByVal Value As String) As Boolean
Dim DP As String
Dim TS As String
' Get local setting for decimal point
DP = Format$(0, ".")
' Get local setting for thousand's separator
' and eliminate them. Remove the next two lines
' if you don't want your users being able to
' type in the thousands separator at all.
TS = Mid$(Format$(10 00, "#,###"), 2, 1)
Value = Replace$(Value, TS, "")
' 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" & DP & "]*" And _
Not Value Like "*" & DP & "*" & DP & "*" And _
Len(Value) > 0 And Value <> DP And _
Value <> vbNullString
End Function

Rick
Mar 1 '06 #8

"code_wrong " <ta*@tac.co.u k> wrote in message
news:44******** **@mk-nntp-2.news.uk.tisca li.com...
Visual Basic (not dot net)
what is the best way to check the User has entered an integer into an
InputBox?

isNumeric() checks for a numeric value .. but does not notify of numbers with
decimal places
inputBox returns a string so I could check for decimal point??? this seems
like overkill

The value returned can be asigned into an Integer type and then a Single type
... the two can be compared .. but still this does not complain about numbers
with decimal places if the fractional part is zero

what is the simple solution?


"simple solution" is always a relative term in this NG :)

For integers, I would do this test, which deals with fractional entries as well:

Private Sub Command1_Click( )
Dim A As Single
Dim B As Long
Dim C As Single
Dim bNum As Boolean
Dim sInput As String

sInput = InputBox("enter an integer", , "0")
bNum = IsNumeric(sInpu t)
If bNum Then
A = CSng(sInput)
B = CLng(A)
C = CSng(B)
End If
If bNum And C = A Then
MsgBox "good dog, you entered " & B
Else
MsgBox "How is " & sInput & " an integer?"
End If

End Sub

That way, the user can enter 3E7 if they want, a perfectly valid way of entering
30,000,000 :)
Mar 1 '06 #9
Steve Gerrard wrote:
MsgBoxÂ*"HowÂ*i sÂ*"Â*&Â*sInput Â*&Â*"Â*anÂ*int eger?"


As a user, seeing sophomoric crap like that get through gives me a very low
opinion of the programmer on the other side.

For the OP; handle errors at keystroke time or at turn-around time (when the
field blurs, or when the current page submits). Then if the error is
complex, write text into a special field on the form. If the error is
simple, beep and turn its field red.

Contact with a user should be as sensitive and gentle as possible, to avoid
looking like the average VB interface.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 1 '06 #10

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

Similar topics

4
2175
by: Gleep | last post by:
Hi PHP coders, I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows. In each row there is a check box - then 4 input fields. I already have the code that inserts all the data BUT the validation is a nightmare. What I need is. If an entire row is empty (not checked or filled out) that's OK. However if a user fills in one or two input fields within a row - that's NOT ok. The entire row needs...
4
4055
by: yai | last post by:
Hi all, I am trying to display close caption with media play by using visual basic.NET I can make it woke on webpage but I don't know how to specific the property mediaplayer.closedCaption.captioningId in visual basic. For the webpage i set mediaplayer.closedCaption.captionindId to the name of text box.
0
1652
by: joe pribele | last post by:
I have this stored procedure that takes some xml as input. What I need to is use xml as a table so that I can join other tables to the xml and get information back that matches the criteria. I can use dxxshredxml to put it into a regular table and everything works great. but when I try to put it into a temp table it doesn't work. I get an error saying Session.ACCESSMASK doesn't have a column name ACCESS_MASK_ID. Do I have to use a...
9
1712
by: Adam Monsen | last post by:
I kindly request a code review. If this is not an appropriate place for my request, where might be? Specific questions are in the QUESTIONS section of the code. ========================================================================== #include <stdio.h> #include <stdlib.h> #include <string.h>
0
1052
by: ntuser_man | last post by:
Howdy I'm trying to validate the content of a csv uploaded to a web page. I imported the csv into a DataSet and now I want to loop through all the elements in the DataSet to validate that each is of the appropriate type. After validation I will loop through the records again and write them to a SQL2000 table using a stored procedure. My plan for validation is as follows. But I'm not entirely sure how to execute the validations. C# has...
2
42891
by: Jim B | last post by:
I am new to VB.NET and have a question on validating user input from a Text Box. I'm writing a small program where I want to check the users input data and determine if it's an Integer or a Decimal. I guess I want to go one step further than just checking for IsNumeric. I have searched this Work Groups' database and did not find exactly what I need. I have tried GetType, VarType, and the Val Function using the Text Box data, without...
3
3366
by: redefined.horizons | last post by:
I'm new to C#, (coming from a Java programming background), and I have a question about the correct use of exceptions. My book "Programming C#" by O'Reilly says the following in Chapter 11, "Handling Exceptions", on page 245: "An error is caused by user action. For example, the user might enter a number where a letter is expected. Once again, an error might cause an exception, but you can still prevent that by catching errors with...
7
16950
by: Xstrain | last post by:
Having difficulty creating error handling when inputting an integer dvdQuant = input("Enter quantity of DVD: ") how can I get python to return a print "Error Message" if an integer is not entered instead of Enter quantity of DVD: gyfj Traceback (most recent call last): File "I:/Python/CourseworkVer3.py", line 203, in <module> addDvd(catalogue) File "I:/Python/CourseworkVer3.py", line 129, in addDvd
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
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
9984
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
9851
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
6662
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
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.