messagebox to show all validation fields

871 views Asked by At

so I have 3 fields that combines into 1 string and I'm currently fixing the validation so the question is how can i identify a certain textbox that is empty and the user needs to fill it up before he/she can proceed i tried this

 if(string.IsNullOrEmpty(txtEYear.Text) || string.IsNullOrEmpty(txtECat.Text) || string.IsNullOrEmpty(txtEID.Text))
                {
                    MessageBox.Show("Please fill in the missing fields");
                }
2

There are 2 answers

0
sujith karivelil On

For that you have to use separate loop and form the message something like this:

bool isValidated = true;
StringBuilder message= new StringBuilder("Please fill the following fields: ");
if(string.IsNullOrEmpty(txtEYear.Text) 
{
  message.Append("Year");
  isValidated = false;
}
if(string.IsNullOrEmpty(txtECat.Text))
{
   message.Append("txtECat");
   isValidated = false;
} 
if(string.IsNullOrEmpty(txtEID.Text))
{
  message.Append("ID");
  isValidated = false;
}

// check all fields are valid
if(isValidated)
{
  // Continue 
}
else
{
    MessageBox.Show(message.ToString());
}                  
0
Shahzad Riaz On

Try this one.When any filed is empty focus the required field.

string message = string.empty;
message = "Please fill the ";
if(string.IsNullOrEmpty(txtEYear.Text))
    {
       message = message + " txtEYear ";
       txtEYear.Focus();
    }
    if(string.IsNullOrEmpty(txtECat.Text))
    {
       message = message + " txtECat";
       txtECat.Focus();
    }
    if(string.IsNullOrEmpty(txtEID.Text))
    {
       message = message + " txtEID";
       txtEID.Focus();
    }

    MessageBox.Show(message+" Fields");