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
  • 21,314
  • 3
  • 24
  • 44
jay_t55
  • 11,362
  • 28
  • 103
  • 174

9 Answers9

4

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

hint 2: try regex

gsharp
  • 27,557
  • 22
  • 88
  • 134
3

int.TryParse( ....

MaLio
  • 2,498
  • 16
  • 23
2

Use regular expression pattern.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

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

amrtn
  • 587
  • 4
  • 10
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,833
  • 3
  • 32
  • 41
1

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

Arjan
  • 19,957
  • 2
  • 55
  • 48
1

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

    if (Regex.IsMatch(yourString, "\\d"))
    {
        // Do your stuff
    }
Canavar
  • 47,715
  • 17
  • 91
  • 122
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
  • 21,214
  • 6
  • 62
  • 67
0

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

Botz3000
  • 39,020
  • 8
  • 103
  • 127