tags:

views:

291

answers:

9

I just have a simple question... How do I check to see if a textbox or a string contains an Integer?

please no code just maybe a hint or two :D

thanks all :)

A: 

A hint - The value in the textox is a string, try to parse it to int and if exception is raised - it is not an integer

EDIT: Actually there is a method which does that - Int32.TryParse

Svetlozar Angelov
Another hint - call a function that tries to parse it and check the return value
Peter van der Heijden
+2  A: 

Use regular expression pattern.

adatapost
Strongly agree on this as this is a good way to validate input
Maciek
I appreciate your comment.
adatapost
+1  A: 

regex (http://en.wikipedia.org/wiki/Regular_expression)

arjan
+4  A: 

hint 1: have a look on the static methods of int... there are 2 methods

hint 2: try regex

gsharp
A: 

use regular expressions to check if the string contains an integer :

    if (Regex.IsMatch(yourString, "\\d"))
    {
        // Do your stuff
    }
Canavar
+2  A: 

Hint: There is a method in Int32 that returns false if passed object is not an integer.

amartin
A: 

you can try int.TryParse or LINQ. The preferable and probably cleanest solution would be a RegEx, though.

Botz3000
+1  A: 

use this regex pattern to validate if the text contains numbers only:

^[0-9]+$

when invalid, means that there is non numeric chars.

Regex regex = new Regex("^[0-9]+$");

regex.IsMatch(textbox1.Text);

Tamir
+3  A: 

int.TryParse( ....

MaLio