473,426 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

Textbox input Validation Question

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,

Lóan
Nov 24 '07 #1
5 1606
QVeen72
1,445 Expert 1GB
Hi,

In C#, the function is :"IsDigit"

you can check this MSDN Link

Regards
Veena
Nov 25 '07 #2
LoanB
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
1,445 Expert 1GB
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
LoanB
62
Will give it a shot thanks!
Nov 26 '07 #5
LoanB
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

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

Similar topics

12
by: Robert | last post by:
As a pre-newbie to visual basic, I haven't programmed for years and I am having the greatest of difficulties coping with it. If any kind soul can help me out with advice then I'd be most grateful....
2
by: Mark | last post by:
I am attempting to populate several textbox controls from VBA code. With each attempt, I get the following error: "The macro or function set to the BeforeUpdate or ValidationRule property for...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
2
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow...
6
by: The Eeediot | last post by:
Hello, Folks... I'm almost becoming a regular to this newsgroup. I am trying to display the contents of an MS-SQL Text field to a TextBox in ASPdotNET. The text in this field contains all...
14
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
0
by: slekshmipriya | last post by:
Hai friends i have a problem with my code.When my form get loaded it will contain the rollno, name and textbox to enter mark of the students.These are fetched from my table named details.The marks...
2
by: =?Utf-8?B?SmVycnkgSg==?= | last post by:
Hello, I am using asp.net 2003 using C#. I am creating textboxes dynamically and want to validate to make sure the user can only input an integer. I don't think I can use the validators...
9
mageswar005
by: mageswar005 | last post by:
Hi Guys, My array textbox validation is not working fine,my code is given below please help me immediately. function validation() { var chks =...
2
by: mrutyunjaya | last post by:
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.