1

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 :)

Hubert Kario
  • 19,585
  • 3
  • 22
  • 41
jay_t55
  • 10,752
  • 26
  • 96
  • 170

9 Answers9

4

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

hint 2: try regex

gsharp
  • 26,016
  • 21
  • 83
  • 126
3

int.TryParse( ....

MaLio
  • 2,400
  • 15
  • 21
2

Use regular expression pattern.

KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
2

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

amrtn
  • 587
  • 4
  • 9
2

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,675
  • 3
  • 30
  • 41
1

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

Arjan
  • 17,401
  • 2
  • 52
  • 47
1

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

    if (Regex.IsMatch(yourString, "\\d"))
    {
        // Do your stuff
    }
Canavar
  • 47,036
  • 17
  • 87
  • 121
0

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
  • 20,444
  • 6
  • 61
  • 67
0

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

Botz3000
  • 38,118
  • 8
  • 102
  • 126