How to check string for alphanumeric, isnumeric, etc [RESOLVED]
I have a text box on my form, for which the input from the user is "correct" if it is a number of things... Certain phrases, and in some cases certain ranges of numbers. When I have the if statements checking the .Text of the textbox, they are using:
C# Code:
if (text1.Text == "test" || System.Convert.ToInt32(text1.Text.Trim()) >= 0)
{
// Do stuff
}
The problem is that if a string or non-number is entered, the conversion throws an exception saying inproper format (I think that is what it said but it is because it is trying to convert a string/non-number to an integer). I have worked around it using try and catch around the if statements, but that can't be the right way, to just ignore the exceptions? If this were VB I would have done isnumeric first to check for a numeric input. What can I do in C#?
Re: How to check string for alphanumeric, isnumeric, etc
If you're using .Net 2.0, then int.TryParse, else you can use double.TryParse [has a parameter where you may specify the numeric format you expect]
Re: How to check string for alphanumeric, isnumeric, etc
I am running 2.0, and that works perfectly! Thanks!