how to get the type of a textbox

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
 
S

Stefan Richter

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
 
S

Scott M.

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

If not, you have to check the text yourself.
 
W

William Ryan eMVP

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.
 
J

John Scalco

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top