Results 1 to 3 of 3

Thread: How to check string for alphanumeric, isnumeric, etc [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    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:
    1. if (text1.Text == "test" || System.Convert.ToInt32(text1.Text.Trim()) >= 0)
    2. {
    3.     // Do stuff
    4. }

    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#?
    Last edited by pjrage; May 22nd, 2007 at 12:27 PM. Reason: RESOLVED

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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]

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    Re: How to check string for alphanumeric, isnumeric, etc

    I am running 2.0, and that works perfectly! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width