By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

Textbox input Validation Question

P: 62
Hi Need some help please. I have some experience in VB.Net and am teaching myself C# now - I must add I know very little about it as yet!

I have a textbox called txtAmount.text

I want to validate the input to see if it is numeric. If not I want a message displayed to state " Please enter a valid numeric amount"

In VB.Net I would do it like this:

If IsNumeric(txtAmount.text) = false Then
MessageBox.show("........")
exit sub *
End If

* I want to exit the sub so that the program does not continue and go on to crash

Now my question is how do I do the above in C#?

I have tried:

1. if (Char.IsNumber(txtAmount.text) - but as txtAmount is a String it does not allow me to do this.

2. IsNumeric - No Luck here..

So please any assistance would be greatly appreciated.

Thanks,

Lan
Nov 24 '07 #1
Share this Question
Share on Google+
5 Replies


QVeen72
Expert 100+
P: 1,445
Hi,

In C#, the function is :"IsDigit"

you can check this MSDN Link

Regards
Veena
Nov 25 '07 #2

P: 62
Hi Veena

Thanks for the feedback, but It's still not working. I have thw=e following code:


if (String.IsNullOrEmpty(txtAmount.Text))
{
if (Char.IsDigit(txtAmount.text))
{
MessageBox.Show("Enter a Valid Purchase Amount", "Input
Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtAmount.Select();
valid = false;
return;
}
}

As the the textbox input is a string Visual Studio won't allow the Char.IsDigit function.

So obviously I have to either convert the input of the textbox to Char type or is threre a better way?

Thanks,
Nov 25 '07 #3

QVeen72
Expert 100+
P: 1,445
Hi,

Write the Code in "OnKeyPress" event.
Check the Link, it Restricts the user from enetering keys other than numbers. Instead of allowing the user to enter everything and then checking in the Lost Focus, Stop the user from entering scrap data.
This is Actual meaning of InputValidation.

Regards
Veena
Nov 26 '07 #4

P: 62
Will give it a shot thanks!
Nov 26 '07 #5

P: 62
Hey Veena

Cheers, but I found it easier to just write a IsNumeric Function. Sure its not perfect, but at least it will check if the input is numeric or not.

Thanks again for the help,
Nov 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.