473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple way to check if a string is a correct integer?

Hi!

Is there a simple way to check if a string entered in a text box is a valid
integer?

Thanks in advance!
/Henke
Nov 15 '05 #1
9 19696
Use a regular expression to match the input with a pattern, say:

Match m=Regex.Match(i nputString,"^[0-9]{1,}$";
if (m.Success) {
//whatever you want to do
}

The pattern above basically says:
[0-9] : check for the occurance of digits 0 to 9
{1,} : these can occur from 1 to infinite times
^ : there must be nothing in front of these digits
$ : there must be nothing behind these digits

success,
Paul
Is there a simple way to check if a string entered in a text box is a valid integer?

Nov 15 '05 #2
Henke <he********@hot mail.com> wrote:
Is there a simple way to check if a string entered in a text box is a valid
integer?


The easiest thing to do is just parse it:

Convert.ToInt32 (myString);

and catch the exception that you get if it's not valid.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
Alternatively you could reference the VB.NET assembly located in the
references dialog and use the IsNumeric function available in VB.NET.

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Henke <he********@hot mail.com> wrote:
Is there a simple way to check if a string entered in a text box is a valid integer?


The easiest thing to do is just parse it:

Convert.ToInt32 (myString);

and catch the exception that you get if it's not valid.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #4
Thnaks for your answer, but on what object should I use IsNumeric? Is it a
static function on some VB data type?

/Henke

"Peter Rilling" <pe***@nospam.r illing.net> skrev i meddelandet
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Alternatively you could reference the VB.NET assembly located in the
references dialog and use the IsNumeric function available in VB.NET.

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Henke <he********@hot mail.com> wrote:
Is there a simple way to check if a string entered in a text box is a valid integer?


The easiest thing to do is just parse it:

Convert.ToInt32 (myString);

and catch the exception that you get if it's not valid.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #5
Henke <he********@hot mail.com> wrote:
Thnaks for your answer, but on what object should I use IsNumeric? Is it a
static function on some VB data type?


You'd call Microsoft.Visua lBasic.Informat ion.IsNumeric(s tring)

You'd also need to add a reference to the Microsoft.Visua lBasic.dll
assembly.

Personally I'd steer clear of using the VB stuff - it makes your code
less portable (eg to Mono) and just feels a bit icky to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Ok, thanks. I think I use the Regex-way...
/Henke

"Jon Skeet" <sk***@pobox.co m> skrev i meddelandet
news:MP******** *************** *@news.microsof t.com...
Henke <he********@hot mail.com> wrote:
Thnaks for your answer, but on what object should I use IsNumeric? Is it a static function on some VB data type?


You'd call Microsoft.Visua lBasic.Informat ion.IsNumeric(s tring)

You'd also need to add a reference to the Microsoft.Visua lBasic.dll
assembly.

Personally I'd steer clear of using the VB stuff - it makes your code
less portable (eg to Mono) and just feels a bit icky to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #7
"Henke" <he********@hot mail.com> wrote in message
news:Oq******** ********@TK2MSF TNGP09.phx.gbl. ..
Is there a simple way to check if a string entered in a text box is a valid integer?


http://aspalliance.com/Ambrose/Articles/IsNumeric.aspx
seems that Double.TryParse (and then check if is in int.MinValue - MaxValue
range) is the fastest

try to avoid exceptions for normal app flow

Nov 15 '05 #8
On Wed, 6 Aug 2003 08:36:05 +0200, "Henke" <he********@hot mail.com> wrote:
Hi!

Is there a simple way to check if a string entered in a text box is a valid
integer?


I just refused to accept anything but digits and backspace!

//
// OnKeyPress - only accept digits..
//
private void OnKeyPress(obje ct sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
e.Handled = !(Char.IsDigit( e.KeyChar) || e.KeyChar == 8); // bksp
}
my email Fg**********@vq k.pbz is encrypted with ROT13 (www.rot13.org)
Nov 15 '05 #9
isNumeric sounds like it would have been a great method to be inside of
String. Can it be added?

Jon Skeet wrote:
Henke <he********@hot mail.com> wrote:

Thnaks for your answer, but on what object should I use IsNumeric? Is it a
static function on some VB data type?


You'd call Microsoft.Visua lBasic.Informat ion.IsNumeric(s tring)

You'd also need to add a reference to the Microsoft.Visua lBasic.dll
assembly.

Personally I'd steer clear of using the VB stuff - it makes your code
less portable (eg to Mono) and just feels a bit icky to me.


--
..tom
remove email address' dashes for replies
opensource middleware at <http://isectd.sourcefo rge.net>
http://gagne.homedns.org
Nov 15 '05 #10

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

Similar topics

3
27563
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
5
3432
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError: unsupported operand type(s) for /=: 'unicode' and 'int' I want to divide x by 10 and assign that value back to x.
9
2735
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string froma file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
3
9195
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
3
1700
by: mattsniderppl | last post by:
Hi, I was hoping someone would know the reasons that this error is given, since i can't find any documentation explaining what causes this. I have a line of code in the onLoad function of my jsp page and everytime i try to reference a variable or function from one of my *.js files i get this error. thanks -matt
232
13297
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
5
5367
by: pnsreee | last post by:
Hi all, im using following perl code to check integer. use Tie::CheckVariables Tie::CheckVariables->on_error(sub{print "ERROR!"}); tie my $scalar,'Tie::CheckVariables','integer'; #$scalar = 88; $scalar = 'test';
3
4629
by: jed | last post by:
in need an example of converting a string to integer
2
3782
by: KWSW | last post by:
Another question to ask about built in java functions. I know that there is a Integer.toBinaryString function where I can get the binary value of an integer in a string representation. Example : from 107 i get "1101011" Question would be, is there a function where I can convert back from string to integer. Like int i = functionName("1101011") where i will contain 107. thanks again.
11
6144
by: luftikus143 | last post by:
Hi there, gush, frustrating. I am not a pro; but I know PHP well enough to understand how it works. And a simple conversion from string to integer via (int)$value or intval($value) should be straightforward. But it's not working in my case. I have a string: % population: 99% which holds some information I extract via preg_match. The resulting variable $r is "99". I have an
0
8889
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
8752
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
9401
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
9179
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
9116
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
6011
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
3
2157
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.