C# Check if a user typed a number into a textbox.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Keevu
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Mar 24, 2009 3:53 pm

C# Check if a user typed a number into a textbox.

Post by Keevu »

Code: Select all

if(IsNumeric(textBox1.Text) == true)
  {
     // your convert code goes here
  }
  else
  {
     // your error message goes here
  }
put something like "please input number" where the your error message goes where comment is at.

snippet made by the great edward. I take no credit. :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: C# Check if a user typed a number into a textbox.

Post by dandymcgee »

Lol, wow. That was simple enough.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: C# Check if a user typed a number into a textbox.

Post by trufun202 »

Keevu wrote:

Code: Select all

if(IsNumeric(textBox1.Text) == true)
  {
     // your convert code goes here
  }
  else
  {
     // your error message goes here
  }
put something like "please input number" where the your error message goes where comment is at.

snippet made by the great edward. I take no credit. :)
actually..that's not correct. .NET doesn't have an IsNumeric() method built into the framework. That's an old VB method that didn't make it in the jump to .NET.

I would recommend the following:

Code: Select all

int number = 0;

if (int.TryParse(textBox1.Text, out number))
{
    //do something with number
}
else
{
    //scream at the user, cuz that's not a number!
}
EDIT: Or, a more user friendly option may be to use an asp:RegularExpressionValidator to validate the number on the client side.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
Keevu
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Mar 24, 2009 3:53 pm

Re: C# Check if a user typed a number into a textbox.

Post by Keevu »

trufun202 wrote:
Keevu wrote:

Code: Select all

if(IsNumeric(textBox1.Text) == true)
  {
     // your convert code goes here
  }
  else
  {
     // your error message goes here
  }
put something like "please input number" where the your error message goes where comment is at.

snippet made by the great edward. I take no credit. :)
actually..that's not correct. .NET doesn't have an IsNumeric() method built into the framework. That's an old VB method that didn't make it in the jump to .NET.

I would recommend the following:

Code: Select all

int number = 0;

if (int.TryParse(textBox1.Text, out number))
{
    //do something with number
}
else
{
    //scream at the user, cuz that's not a number!
}
EDIT: Or, a more user friendly option may be to use an asp:RegularExpressionValidator to validate the number on the client side.


thanks this helped it works very well. :)
Apoc
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Fri Jul 31, 2009 5:52 am

Re: C# Check if a user typed a number into a textbox.

Post by Apoc »

Use decimal.TryParse unless you want to force non-floating-point numbers on the user. (In which case; I'd still use a larger datatype. long.TryParse for instance)

Also, don't use TryParse unless you're absolutely sure you want to suppress exceptions. (In simple cases like this, it's fine. In most other cases, avoid it wherever possible.)
Post Reply