473,473 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I validate a textbox.text is integer?

Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code this
in the textbox_validating event but I don't know what function to call to
make sure the input are all integer nubmers.

Thanks, Alpha
Nov 17 '05 #1
4 20586
I don't think there is a method (at least not in .NET 1.1). You can either
use a regular expression, or try to convert the string to an integer and
wrap it in a try...catch block.

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code
this
in the textbox_validating event but I don't know what function to call to
make sure the input are all integer nubmers.

Thanks, Alpha

Nov 17 '05 #2
One of the easiest ways would be to call a function like this from your event
handler that is handling the Validating event:

private bool IsInt(TextBox textbox)
{
try
{
int i = int.Parse(textBox1.Text);
return true;
}
catch
{
return false;
}
}

To call IsInt, you pass in a reference to the TextBox in question and it
attempts to parse the value in the Text property as an integer, if it
succeeds (ie doesn’t raise an exception), it returns true. If it fails
however (ie it was unable to parse it as an int), it will raise an exception
internally and return false.

There are other ways you could do this (ie not involving raising of
exceptions willy nilly), like making sure that the only numeric characters
are in the string, or using Regular Expressions... however as I said, this is
really the quickest and easiest way (code wise) to do it.

Brendan
"Alpha" wrote:
Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code this
in the textbox_validating event but I don't know what function to call to
make sure the input are all integer nubmers.

Thanks, Alpha

Nov 17 '05 #3
Thank you very much.

"Brendan Grant" wrote:
One of the easiest ways would be to call a function like this from your event
handler that is handling the Validating event:

private bool IsInt(TextBox textbox)
{
try
{
int i = int.Parse(textBox1.Text);
return true;
}
catch
{
return false;
}
}

To call IsInt, you pass in a reference to the TextBox in question and it
attempts to parse the value in the Text property as an integer, if it
succeeds (ie doesn’t raise an exception), it returns true. If it fails
however (ie it was unable to parse it as an int), it will raise an exception
internally and return false.

There are other ways you could do this (ie not involving raising of
exceptions willy nilly), like making sure that the only numeric characters
are in the string, or using Regular Expressions... however as I said, this is
really the quickest and easiest way (code wise) to do it.

Brendan
"Alpha" wrote:
Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code this
in the textbox_validating event but I don't know what function to call to
make sure the input are all integer nubmers.

Thanks, Alpha

Nov 17 '05 #4
Thank you.

"Peter Rilling" wrote:
I don't think there is a method (at least not in .NET 1.1). You can either
use a regular expression, or try to convert the string to an integer and
wrap it in a try...catch block.

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code
this
in the textbox_validating event but I don't know what function to call to
make sure the input are all integer nubmers.

Thanks, Alpha


Nov 17 '05 #5

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

Similar topics

3
by: rodger | last post by:
How do i validate text box entry, in VB. net In the event that user does not make a text entry, simply pop-up a warning, thanks rodger
15
by: simonmarkjones | last post by:
I want to validate my form using a BeforeUpdate event. However now that i call my code with a beforeupdate it wont let me go to next or previous records. What code should i put in o allow me...
7
by: Rich | last post by:
Hello, I have 10 textboxes on a form. I would like to validate text-data entry on each textbox. I was looking for a Form_Current event or Form_Change event but did not recognize any such...
9
by: B-Dog | last post by:
I have a form that has about 10 text boxes on it, they all have to be filled out before submitting is there a quick way to make sure that none are null or do I have to call out each textbox? Say...
2
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's...
3
by: Jeff Jarrell | last post by:
I have one textbox on a form. Nothing else. The validate event doesn't fire. Drop another textbox on the form and the validate event fires. Set tabstop = false on the 2nd text box and the...
5
by: YardDancer | last post by:
Hi all I don't want to use the the Cint() or CDec() or other VB.Net Functions and I dont want to restrict keystrokes in the textbox. I am also using the "strict type semantic" settings that...
2
Echooff3
by: Echooff3 | last post by:
I'm ready to pull my hair out with this!!! I'm trying to validate a textbox so that only a Integer or a Decimal can be entered. If I enter a 1 in the box it validates fine and the focus moves to the...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
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
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,...
0
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...
1
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...
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.