how to get the type of a textbox

  • Thread starter Thread starter Stefan Richter
  • Start date Start date
S

Stefan Richter

Hi,

how can I get the type of what was entered into a textbox?
I want to validate the user inputs, so I would like to know
if what the user entered was a number or not.

thanks,

Stefan
 
Okay,

so I really have to do that all by myself,
character for character??
???

Isn't there some function that automatically does it?

Thanks,

Stefan
 
In a web form, there are validation controls that do the job for you.

If not, you have to check the text yourself.
 
It's really simple, keypress only passes one character at a time, so you can
just check the current key. If it's a number for isntance, ignore it.

like Scott mentioned, if you are using ASP.NET you can use the Regex
validators. Similarly, you can easily build this in on the desktop
controls.
 
You can do this with regular expressions as mentioned in other replies to
your post.
Or you can create a function, say StringToInt("42");

Here's some code which does pretty much what StringToInt() might do

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
string s = textBox1.Text;

int num = IsNumber(s);
}

private bool IsNumber (string astr)
{
try {
int i = Convert.ToInt32(astr);
return (true);
}
catch (System.Exception) {
return (false);
}
}

maybe something like that?

hth,
J
 
Back
Top