Here is the code:
if (roomGender == "M")
{
if (gender == "F")
{
row.Cells[5].BackColor = Color.FromName("#FF0000");
args.IsValid = false;
vldGender.ErrorMessage = building +" " + room + ": You cannot place a female in this space";
}
else
{
vldGender.ErrorMessage = "";
}
}
//end male gender check
//Female gender check
if (roomGender == "F")
{
if (gender == "M")
{
row.Cells[5].BackColor = Color.FromName("#FF0000");
args.IsValid = false;
vldGender.ErrorMessage = building +" " + room + ": You cannot place a male in this space";
}
else
{
vldGender.ErrorMessage = "";
}
}
//end female gender check
//Validate Names
string last = ((TextBox)row.FindControl("txtLast")).Text;
string first = ((TextBox)row.FindControl("txtFirst")).Text;
if (last == "" && first != "")
{
row.Cells[3].BackColor = Color.FromName("#FF0000");
args.IsValid = false;
vldLast.ErrorMessage = building +" " + room + ": The last name cannot be blank";
}
else
{
vldLast.ErrorMessage = "";
}
if (last != "" && first == "")
{
row.Cells[4].BackColor = Color.FromName("#FF0000");
args.IsValid = false;
vldFirst.ErrorMessage = building +" " + room + ": The first name cannot be blank";
}
else
{
vldFirst.ErrorMessage = "";
}
if (last != "" && first != "" && gender == "")
{
row.Cells[5].BackColor = Color.FromName("#FF0000");
args.IsValid = false;
vldGender2.ErrorMessage = building +" " + room + ": A gender must be selected";
}
else
{
vldGender2.ErrorMessage = "";
}
if (!(regLast.IsValid))
{
row.Cells[3].BackColor = Color.FromName("#FF0000");
regLast.ErrorMessage = building +" " + room + ": The last name is incorrect, please check the name";
}
if (!(regFirst.IsValid))
{
row.Cells[4].BackColor = Color.FromName("#FF0000");
regFirst.ErrorMessage = building +" " + room + ": The first name is incorrect, please check the name";
}
}
}
}
My problem
Because this is using if statements when one of the if statements fails to validate, the if statement stops on the line. So the rest of the Fields on that line aren’t validated.
I have the field first name, and last name and gender.
If I forget to add in the first name, and the last name, but not the gender.
This validation will only show me missing the first name, the last name wouldn’t’ be checked until after the first name is fixed.
Is their any way to solve this issue, so that both field are checks at the same time?
This is a classic case where the code is getting too complicated for one function.
It will become much more readable and maintainable if you validate each field as an independent step.