JayP20
JayP20

Reputation: 37

Simple input validation in C#

I am trying to add input validation to a text box to make sure that if the user enter anything other than a number one of my labels will say "Input a number only." I have tried using an 'if' statement for this (look at code below) but it did not work. Instead of posting the message to "input a number" my program crashes with this error message

'System.FormatException': Input string was not in a correct format

I am a noob so I'm not sure how to fix this.

namespace Calculator { public partial class frmCalculator : Form { public frmCalculator() { InitializeComponent(); }

    string name = "";//Stores the Name typed into the text box
    string classType = "";//Stores the Class typed into the text box
    decimal grade;// Stores the Grade typed into the text box


    private void btnSubmit_Click(object sender, EventArgs e)
    {

        classType = txtBoxClass.Text;
        grade = Convert.ToDecimal(txtBoxGrade.Text);
        name = txtBoxName.Text;

        switch (classType)
        {
            //Case for Math class and grade comparison
            case "m":
            case "M":
                if (grade >= -1 && grade <= 100)
                {
                    if (grade >= 94)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A";
                    }
                    if (grade <= 93)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A-";
                    }
                    if (grade <= 89)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B+";
                    }
                    if (grade <= 86)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an B";
                    }
                    if (grade <= 83)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B-";
                    }
                    if (grade <= 79)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C+";
                    }
                    if (grade <= 76)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C";
                    }
                    if (grade <= 73)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C-";
                    }
                    if (grade <= 69)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a D";
                    }
                    if (grade < 65)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an F";
                    }

                    //Clears the text boxes when the Submit button is clicked.
                    txtBoxName.Text = "";
                    txtBoxClass.Text = "";
                    txtBoxGrade.Text = "";
                }
                else { lblAnswer.Text = "Input a number!"; }
                break;

            //Case for Science class and grade comparison
            case "s":
            case "S":
                if (grade >= 94)
                {
                    lblAnswer.Text = name + "'s grade in Science is an A";
                }
                if (grade <= 93)
                {
                    lblAnswer.Text = name + "'s grade in Science is an A-";
                }
                if (grade <= 89)
                {
                    lblAnswer.Text = name + "'s grade in Science is a B+";
                }
                if (grade <= 86)
                {
                    lblAnswer.Text = name + "'s grade in Science is an B";
                }
                if (grade <= 83)
                {
                    lblAnswer.Text = name + "'s grade in Science is a B-";
                }
                if (grade <= 79)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C+";
                }
                if (grade <= 76)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C";
                }
                if (grade <= 73)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C-";
                }
                if (grade <= 69)
                {
                    lblAnswer.Text = name + "'s grade in Science is a D";
                }
                if (grade < 65)
                {
                    lblAnswer.Text = "'s grade in Science is an F";
                }

                //Clears the text boxes when the Submit button is clicked.
                txtBoxName.Text = "";
                txtBoxClass.Text = "";
                txtBoxGrade.Text = "";

                break;

            //Case for English class and grade comparison.
            case "e":
            case "E":
                if (grade >= 94)
                {
                    lblAnswer.Text = name + "'s grade in English is an A";
                }
                if (grade <= 93)
                {
                    lblAnswer.Text = name + "'s grade in English is an A-";
                }
                if (grade <= 89)
                {
                    lblAnswer.Text = name + "'s grade in English is a B+";
                }
                if (grade <= 86)
                {
                    lblAnswer.Text = name + "'s grade in English is an B";
                }
                if (grade <= 83)
                {
                    lblAnswer.Text = name + "'s grade in English is a B-";
                }
                if (grade <= 79)
                {
                    lblAnswer.Text = name + "'s grade in English is a C+";
                }
                if (grade <= 76)
                {
                    lblAnswer.Text = name + "'s grade in English is a C";
                }
                if (grade <= 73)
                {
                    lblAnswer.Text = name + "'s grade in English is a C-";
                }
                if (grade <= 69)
                {
                    lblAnswer.Text = name + "'s grade in English is a D";
                }
                if (grade < 65)
                {
                    lblAnswer.Text = name + "'s grade in English is an F";
                }

                //Clears the text boxes when the Submit button is clicked.
                txtBoxName.Text = "";
                txtBoxClass.Text = "";
                txtBoxGrade.Text = "";

                break;

            default:
                lblAnswer.Text = "Invalid Class type! Enter M for Math, S for Science or \nE for English only.";
                txtBoxClass.Text = "";//Clears just the Class text box.

                break;
    }

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        //Exits the application when the Exit button is clicked.
        this.Close();
    }

}

}

Upvotes: 2

Views: 1356

Answers (4)

Mukul Varshney
Mukul Varshney

Reputation: 3141

Try below. You can make the below code more better. Anyway, keeping improvement as close to your code, there are couple of improvement you can make:-

  1. Use of TryParse to check if grade is a valid decimal or not.
  2. Use else if.
  3. The condition if (grade > -1 && grade <= 100), now checks for decimal between 0 and 100. Earlier it was allowing -1 too. It that's your requirement then please change the condition accordingly.

Below code will assist you.

private void btnSubmit_Click(object sender, EventArgs e)
{
    var classType = txtBoxClass.Text;
    var name = txtBoxName.Text;

    decimal grade;
    if (! decimal.TryParse(txtBoxGrade.Text, out grade))
    {
        lblAnswer.Text = "Input a number!";
        return;
    }            

    switch (classType)
    {
        //Case for Math class and grade comparison
        case "m":
        case "M":
            if (grade > -1 && grade <= 100)
            {
                if (grade >= 94)
                {
                    lblAnswer.Text = name + "'s grade in Math is an A";
                }
                else if (grade <= 93)
                {
                    lblAnswer.Text = name + "'s grade in Math is an A-";
                }
                else if (grade <= 89)
                {
                    lblAnswer.Text = name + "'s grade in Math is a B+";
                }
                else if (grade <= 86)
                {
                    lblAnswer.Text = name + "'s grade in Math is an B";
                }
                else if (grade <= 83)
                {
                    lblAnswer.Text = name + "'s grade in Math is a B-";
                }
                else if (grade <= 79)
                {
                    lblAnswer.Text = name + "'s grade in Math is a C+";
                }
                else if (grade <= 76)
                {
                    lblAnswer.Text = name + "'s grade in Math is a C";
                }
                else if (grade <= 73)
                {
                    lblAnswer.Text = name + "'s grade in Math is a C-";
                }
                else if (grade <= 69)
                {
                    lblAnswer.Text = name + "'s grade in Math is a D";
                }
                else if (grade < 65)
                {
                    lblAnswer.Text = name + "'s grade in Math is an F";
                }

                //Clears the text boxes when the Submit button is clicked.
                txtBoxName.Text = "";
                txtBoxClass.Text = "";
                txtBoxGrade.Text = "";
            }
            else { lblAnswer.Text = "Input a number between 0 and 100!"; }
            break;
    }
}

Upvotes: 0

Kip Morgan
Kip Morgan

Reputation: 738

You can see if it is a number by using TryParse.

MSDN Decimal.TryParse

decimal grade;
if(!decimal.TryParse(txtBoxGrade.Text, out grade))
{
     MessageBox.Show(string.Format("Unable to parse '{0}'.", txtBoxGrade.Text));
     return;
}

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65107

You should use the TryParse methods of the numerical primitives to see if converting input to a number is possible.

decimal grade;
if (Decimal.TryParse(txtBoxGrade.Text, out grade)) {
    // Its a valid number - the rest of your code goes here
    // and uses the grade variable as the number you want.
} else {
    // Its not a valid number
    lblAnswer.Text = "Input a number only.";
}

Upvotes: 6

NineBerry
NineBerry

Reputation: 28549

Use Decimal.TryParse to check whether the input text is a decimal without throwing an exception if it is not.

if(Decimal.TryParse(txtBoxGrade.Text, out grade))
{
   // Your code
}
else
{
   lblAnswer.Text = "Input a number!";
}

Upvotes: 1

Related Questions