1

I am trying to check if a textbox contains a number. The problem is that it always returns that it contains a non-numeric character. I've tried several ways, but none of them seems to work.

One of the ways I've tried is:

if( Regex.IsMatch(tb.Text.Trim(), @"^[0-9]+$")) // tb.Text is the textbox 

It does not matter what I enter in the textbox, it always returns that it contains a non-numeric character (I tried entering 1-9, 'a', 'b')

wesley221
  • 67
  • 2
  • 10
  • Is it only one character in the textbox – sshashank124 Apr 14 '14 at 11:02
  • Yes, I tried it with just 1 character and with multiple characters – wesley221 Apr 14 '14 at 11:03
  • Why do you use such a complicated regex instead of "\d"? See http://stackoverflow.com/questions/3180354/regex-check-if-string-contains-at-leat-one-digit – Blackunknown Apr 14 '14 at 11:06
  • Do you want to parse the `TextBox` value or do you want to check if it contains numbers? – Guilherme Oliveira Apr 14 '14 at 11:15
  • I want to check if the textbox contains any number, if it does not contain any number I want it to give an error. Therefore i tried this: `if( Regex.IsMatch(tb.Text.Trim(), @"\d")) { Debug.WriteLine(tb.Text); containsLetter = true; break; }` If i enter 3 in the textbox, it returns the value 3 in the debug window. – wesley221 Apr 14 '14 at 11:27
  • @wesley221 I don't know what validation you're doing before calling this Regex, cause for me it worked. Did you check if your `TextBox` contains any value? – Guilherme Oliveira Apr 14 '14 at 11:29
  • @GuilhermeOliveira I know for sure that the textbox isnt empty, since the debug window shows me the value from the textbox (also I just added a tb.Text != isnull to the statement, this didn't affect the result). Before the RegEx I don't have any validation, so as far as I know that cant be the problem. – wesley221 Apr 14 '14 at 11:33
  • @wesley221 This `Regex` should work. Did you try `abc123` to see if it matches? – Guilherme Oliveira Apr 14 '14 at 11:45
  • @wesley221 "test1test" would be giving an error or passing? – Blackunknown Apr 14 '14 at 11:47
  • 1
    your regex fails because the way you put it you declare that it shall only be numbers as indicated by ^(start) and $(end). if you leave out that bit it should work – Dbl Apr 14 '14 at 12:13

4 Answers4

4

You could just parse the string to a specific number type, i.e.

double result;
if (!double.TryParse(tb.Text, out result))
{
  //text is not a valid double;
  throw new Exception("not a valid number");
}
//else the value is within the result variable

From your regex it seems that you need only integer values, so you should use int.TryParse or long.TryParse instead.


Quick and dirty test program:

void Main()
{
    TestParse("1");
    TestParse("a");
    TestParse("1234");
    TestParse("1a");
}

void TestParse(string text)
{
  int result;
  if (int.TryParse(text, out result))
  {
    Console.WriteLine(text + " is a number");
  }
  else
  {
    Console.WriteLine(text + " is not a number");
  }
}

Results:

1 is a number 
a is not a number  
1234 is a number  
1a is not a number
SWeko
  • 30,434
  • 10
  • 71
  • 106
2

You could replace your Regex for this:

if(Regex.IsMatch(tb.Text.Trim(), @"[0-9]"))

Or for this:

if(Regex.IsMatch(tb.Text.Trim(), @"\d"))
Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
1

you can use TryParse:

int value;

bool IsNumber = int.TryParse(tb.Text.Trim(), out value);

if(IsNumber)
{
    //its number
}
helb
  • 7,609
  • 8
  • 36
  • 58
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1
private void btnMove_Click(object sender, EventArgs e)
        {
            string check = txtCheck.Text;
            string status = "";
            for (int i = 0; i < check.Length; i++)
            {
                if (IsNumber(check[i]))
                status+="The char at "+i+" is a number\n";
            }
            MessageBox.Show(status);
        }
        private bool IsNumber(char c)
        {
            return Char.IsNumber(c);
        }
HackerMan
  • 904
  • 7
  • 11