regex - c# check for integers in a textbox -


i trying check if textbox contains number. problem returns contains non-numeric character. i've tried several ways, none of them seems work.

one of ways i've tried is:

if( regex.ismatch(tb.text.trim(), @"^[0-9]+$")) // tb.text textbox  

it not matter enter in textbox, returns contains non-numeric character (i tried entering 1-9, 'a', 'b')

you parse string specific number type, i.e.

double result; if (!double.tryparse(tb.text, out result)) {   //text not valid double;   throw new exception("not valid number"); } //else value within result variable 

from regex seems need integer values, should use int.tryparse or long.tryparse instead.


quick , 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 + " number");   }   else   {     console.writeline(text + " not number");   } } 

results:

1 number  not number   1234 number   1a not number 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -