2

I want to create an if Statement that validates whether an input number in a textbox is between 0 to 100. For example:

NumericUpDown num = new NumericUpDown();

num.Maximum = 100;
num.Minimum = 0;

if (int.Parse(txtMid.Text) < num.Minimum && int.Parse(txtMid.Text) > num.Maximum)
{
    MessageBox.Show("Please input 0 to 100 only.");
}

That's all. Thanks in advance.

4
  • Do you mean || instead of && and txtbox1 instead of txtbox2? Commented Nov 14, 2011 at 11:44
  • Ever thought about using NumericUpDown? Commented Nov 14, 2011 at 11:51
  • @Rawling It's both txtbox1. Sorry. Commented Nov 14, 2011 at 11:54
  • 1
    Replace the && with || and you should be fine. That if always returns false because a number cannot be < 0 and > 100 at the same time. Commented Nov 14, 2011 at 12:13

6 Answers 6

6

You need to parse the txtbox1.Text string into an integer:

int val = 0;
bool res = Int32.TryParse(txtbox1.Text, out val);
if(res == true && val > -1 && val < 101)
{
    // add record
}
else
{
    MessageBox.Show("Please input 0 to 100 only.");
    return;
}

Also, do you need to test one or two textboxes? If it's only one and you need the interval 0, 100, then your condition is wrong, because it always returns false (a number cannot be at the same time <= -1 and >= 101).

VERY IMPORTANT: I have reversed your if/else: you have to print the error in the else and add the record in the if.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you update your original post with the current code that you have?
2

First convert the text into Int then you can compare it. But be careful the entered text might not be the number for example user can enter abc instead of 12

You can use Int.TryParse(String) method to check whether the textbox contains valid number or not. If the number is valid then apply the if condition below

if(Convert.ToInt32(txtbox1.Text) <= -1 && Convert.ToIn32(textbox2.Text >= 101)
{

}

Comments

1

The other answers answer your immediate question but a better option for entering a number is to use the NumericUpDown control that limits the input to numbers, and provides a spin control.

Comments

1

Essentially what you want to do is the following:

  • Get the current text from the textbox.
  • Convert the text to a number (using the TryParse method for the type).
  • If the text could not be converted to a number, notify the user and restore the text box to the last known good value (or an empty string, if no such value present). If you don't do this you will leave the text box in an invalid state, which is probably not what you want.
  • If the text could be converted, check that it is within the given range. If it is, store it as the last known good value for later use. If not, notify the user and restore the text box to the last known good value.

Comments

0

TextBox.Text property will return value as a string. So, first you have to parse the that string into integer. You can use following code :

int num = -2;
bool conversionSuccessful = int.tryParse(txtbox1.Text, out num);
if(conversionSuccessful) {
    if (num <= -1 && num >= 101) {
        MessageBox.Show("Please input 0 to 100 only.");
    return;
    }
}

You can find more information about conversion here and here

4 Comments

Shouldn't you test if the conversion was actually successful?
That'd be a clever way of handling the invalid value if it worked, but if TryParse fails it sets the int to 0.
@Tudor, you are right, answer edited to check the conversion result.
Also, that if always returns false.
0
int val1=0,val2=0
try
{
val1 = Convert.ToInt32(txtbox1.Text);
val2 = Convert.ToIn32(textbox2.Text); 
}
catch
{
MessageBox.Show("Invalid Input");
}

// Do all the Checking here....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.