0

As I've tried with the code below but it did not work. I would like to use Regular Expression for only whole numbers.

if (Regex.IsMatch(tbColumn.Text, @"^[0-9]") == true)
{
    MessageBox.Show("true");
}
else
{
    MessageBox.Show("false");
}

With my code:

0 --> true
1 --> true
9 --> true
10 --> false (it must be true)
100 --> false (it must be true)

For example:

0 --> true
1 --> true
100 --> true
34343 --> true
0.5 --> false
1.42 --> false
1,2 --> false
a1 --> false
a --> false
PMay 1903
  • 1,103
  • 3
  • 15
  • 31

1 Answers1

1

You need to place a quantifier after your character class and add the end of string $ anchor to the regex.

^[0-9]+$

The + quantifier matches the preceding token "one or more" times.

hwnd
  • 69,796
  • 4
  • 95
  • 132