locked
Textbox input validation RRS feed

  • Question

  • Hello all,

    C# Beginner needs help

    I have wrote a small exercise program with 4 text boxes which required user to input only one of the 2 strings: y or n

    Could someone kindly show me how to do a simple input validation so only y or n are valid responses

    This is what I've done

                if ((ans1Box.Text != "y") || (ans1Box.Text != "n") ||
                    (ans2Box.Text != "y") || (ans2Box.Text != "n") ||
                    (ans3Box.Text != "y") || (ans2Box.Text != "n") ||
                    (ans4Box.Text != "y") || (ans4Box.Text != "n"))
                {
                    MessageBox.Show("Please answer y or n in all 4 textboxes!");
                    ans1Box.Text = null;
                    ans2Box.Text = null;
                    ans3Box.Text = null;
                    ans4Box.Text = null;
                    ans1Box.Focus();
                }
                else
                {
                    RainingOrNot();
                }

    All above codes under button click, the message still pops up even when the input IS correct . What did I do wrong here?

    Thanks for advise


    Wednesday, April 29, 2009 11:10 AM

Answers

  • You'll have to use &&, not ||.  Write a little helper function to keep it readable:

      private static bool InvalidAnswer(string txt) {
        return txt != "y" &&  txt != "n";
      }

    ...
      if (InvalidAnswer(ans1Box.Text) || InvalidAnswer(ans2Box.Text) ...


    Hans Passant.
    Wednesday, April 29, 2009 12:23 PM
    Moderator

All replies

  • You'll have to use &&, not ||.  Write a little helper function to keep it readable:

      private static bool InvalidAnswer(string txt) {
        return txt != "y" &&  txt != "n";
      }

    ...
      if (InvalidAnswer(ans1Box.Text) || InvalidAnswer(ans2Box.Text) ...


    Hans Passant.
    Wednesday, April 29, 2009 12:23 PM
    Moderator
  • How about handling the KeyPress Event

    1. Make the textbox length to 1

    2. For all the ans*Box handle the event KeyPress, assign the same handler

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                switch (e.KeyChar)
                {
                    case 'y':
                        break;
                    case 'Y':
                        break;
                    case 'n':
                        break;
                    case 'N':
                        break;
                    case '\b':
                        break;
                    default:
                        MessageBox.Show("Enter only Y/N");
                        e.Handled = true;
                        break;
                }
            }

    3. Now handle the checking as
    if(String.IsNullOrEmpty(ans1Box.Text) || .....)
    {
    }
    else
    {
    }

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Wednesday, April 29, 2009 12:54 PM
  • nobugz is the master of custom controls, perhaps he can help you create your own textbox which only allows a y or a n

    public class CustomTextBox : System.Windows.Forms.TextBox
    {
          protected override void OnKeyPress(KeyPressEventArgs e)
          {
                base.OnKeyPress(e);
                if (e.KeyChar.ToString() != "y"))
                    e.Handled = true;
          }
    }
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, April 29, 2009 5:33 PM
  • Use a CheckBox.
    Hans Passant.
    Wednesday, April 29, 2009 7:30 PM
    Moderator
  • Here u go,

                    if ((ans1Box.Text != "y") && (ans1box.Text != "n") ||
                      (ans2Box.Text != "y") && (ans2Box.Text != "n") ||
                    (ans3Box.Text != "y") && (ans3Box.Text != "n") ||
                    (ans4Box.Text != "y") && (ans4Box.Text != "n"))
                 
                {
                   MessageBox.Show("Please answer y or n in all 4 textboxes!");
                    ans1Box.Text = null;
                    ans2Box.Text = null;
                    ans3Box.Text = null;
                    ans4Box.Text = null;
                    ans1Box.Focus();
                }
                else
                {
                    RainingOrNot();
                }

    • Proposed as answer by Amir Maddah Saturday, May 2, 2009 4:45 PM
    Saturday, May 2, 2009 4:44 PM