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 :)
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 :)
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
hint 1: have a look on the static methods of int... there are 2 methods
hint 2: try regex
use regular expressions to check if the string contains an integer :
if (Regex.IsMatch(yourString, "\\d"))
{
// Do your stuff
}
Hint: There is a method in Int32 that returns false if passed object is not an integer.
you can try int.TryParse
or LINQ. The preferable and probably cleanest solution would be a RegEx, though.
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);