hi all,
ok i am creating some programs (trying to learn visual C#) but i am abit stuck on how to validate a users input. I have the text box, but i want to be able to check to make sure that they are entering a integer and not characters.
How is it i can do this?
Many Thanks,
J
19 replies to this topic
#1
Posted 01 October 2010 - 09:00 AM
|
|
|
#2
Posted 01 October 2010 - 09:26 AM
There's actually no easy way doing this. You have to check each character if it's numeric or not.
bool isNumeric(String str)
{
bool valid = true;
bool seperator = false;
foreach (Char c in str.Trim())
{
if (!char.IsNumber(c))
{
if (c == '.' && !seperator)
{
seperator = true;
}
else
{
valid = false;
break;
}
}
}
return valid;
}
#3
Posted 01 October 2010 - 01:33 PM
There is an easy way, use a MaskedTextBox instead. Set the Mask property to whatever you need. For example, if the number must be 5 digits long you'd use "00000". If it could be up to 5 digits in length you would use "99990" (9 means optional digit, 0 means required digit). You can read more about it at the provided link.
You could also attach a handler to the Validating event and validate the entire input. Or attach a handler to the keypress event and validate the individual keypresses.
It all depends on what you need to do and how you want to do it.
You could also attach a handler to the Validating event and validate the entire input. Or attach a handler to the keypress event and validate the individual keypresses.
It all depends on what you need to do and how you want to do it.
#4
Posted 01 October 2010 - 01:35 PM
That will only work if you only allows a few number of digits, good luck writing in the whole mask for 2 billion characters. :D
#5
Posted 01 October 2010 - 01:38 PM
Vswe said:
That will only work if you only allows a few number of digits, good luck writing in the whole mask for 2 billion characters. :D
He said an integer, so not going to be more than a ten digit mask.
#6
Posted 01 October 2010 - 01:40 PM
It will still be an integer if it has 2 billion chars. It won't be an Int32, but still an integer.
#7
Posted 02 October 2010 - 07:30 AM
There are a few ways to do this as you have seen already. My favorite was the already mentioned validator attached to an event handler. You can do this either for each keystroke (wich is cool but inefficient) or on the final submit buttons click handler event (or other control) .
private void Button1_Click(object sender, EventArgs e)
{
try
{
int.Parse(TXT_Number.Text);
}
catch
{
LBL_NumberError.Text = "You have not entered a number";
}
}
The code above validates the number and sends an error message to a label if the format is incorrect. This way isn't any better but you do get an exception back, wich can be displayed to the user so they know they typed something in wrong and must re-enter the info. Plus I think you will find this to be the most commonly used method.
#8
Posted 02 October 2010 - 08:06 AM
gaylo565 said:
The code above validates the number and sends an error message to a label if the format is incorrect. This way isn't any better but you do get an exception back, wich can be displayed to the user so they know they typed something in wrong and must re-enter the info. Plus I think you will find this to be the most commonly used method.
private void Button1_Click(object sender, EventArgs e) {
int r;
if (Int32.TryParse(TXT_Number.Text, out r)) {
// value is a number and is stored in r
} else {
// value is not a number ... do what you need
}
}
#9
Posted 02 October 2010 - 10:49 AM
Momerath said:
It is very common, but it's wasteful of resources. Never throw an exception as a logic test:
#10
Posted 02 October 2010 - 10:38 PM
gaylo565 said:
lol...This is not a logic test??? This is to check user input, and an exception is a pretty good way to go, for the reason I already gave.
How is it not a logic test? You are 'asking' if the number is an integer, if it isn't, you throw an exception. Bad practice.
#11
Posted 03 October 2010 - 07:47 AM
I have to agree, it's very bad practice to use exceptions as a logic test.
#12
Posted 03 October 2010 - 05:38 PM
Ok...there are certainly more itterations caused by throwing exceptions and thus it is a more resource intensive task. An exception isn't the most efficient way. As for not using exceptions for logic tests what is a divide by zero exception if it isn't an if statement checking if the denominator is zero?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









