0

这是代码:

            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";
            }
        }
    }
}

我的问题 因为这是在 if 语句之一无法验证时使用 if 语句,所以 if 语句停止在该行上。因此,该行的其余字段未经过验证。

我有字段名,姓氏和性别。

如果我忘记添加名字和姓氏,但没有添加性别。

此验证只会显示我缺少名字,直到名字被固定后才会检查姓氏。

他们有什么方法可以解决这个问题,以便同时检查两个字段吗?

4

2 回答 2

3

这是一个经典案例,代码对于一个函数来说太复杂了。

如果您将每个字段作为一个独立的步骤进行验证,它将变得更具可读性和可维护性。

void ValidateRoomGender() 
{
     if(string.IsNullOrEmpty(roomGender))
     {
         vldRoomGender = "Please enter a room gender";
     }
     else if ((roomGender != 'M') && (roomGender != 'F'))
     {
         vldRoomGender = "Invalid Value";
     }
     else
     {
         vldRoomGender = string.Empty;
     }
}

void ValidateGender() 
{
     if( ((vldGender == 'F') && (vldRoomGender == 'M')) || ((vldGender == 'M') && (vldRoomGender == 'F'))
     {
          vldGender = "The gender must match the room"
     }
     else if (string.IsNullOrEmpty(vldGender))
     {
        // etc
     }
}


void Validate()
{
      ValidateRoomGender();
      ValidateGender();
      ValidateFirstName();
      ValidateSurname();
}
于 2012-05-01T23:16:00.657 回答
0

我不确定它会在验证失败的地方返回,但我已经重新组织了您的代码以更高效和更易于维护。vldGender 和 vldGender2 有原因吗?regFirst 和 regLast 做什么?

此外,在比较字符串值时应避免使用 == 和 !=。检查 "" 或 null 时,应使用 string.Equals 或 string.IsNullOrEmpty。我还可以为您的一些错误消息推荐 String.Format 吗?

if (roomGender.Equals("M"))
{
    if (gender.Equals("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 = "";
        vldGender2.ErrorMessage = "";
    }

}
//end male gender check
//Female gender check
else if (roomGender.Equals("F"))
{
    if (gender.Equals("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 = "";
        vldGender2.ErrorMessage = "";
    }
}
//end female gender check
// No gender selected
else
{
    row.Cells[5].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldGender2.ErrorMessage = building + " " + room + ": A gender must be selected";
}

//Validate Names
string last = ((TextBox)row.FindControl("txtLast")).Text;
string first = ((TextBox)row.FindControl("txtFirst")).Text;

if (string.IsNullOrEmpty(last))
{
    row.Cells[3].BackColor = Color.FromName("#FF0000");
    args.IsValid = false;
    vldLast.ErrorMessage = building + " " + room + ": The last name cannot be blank";
}
else            
    vldLast.ErrorMessage = "";

if (string.IsNullOrEmpty(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 (!(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";
}
于 2012-05-01T23:04:13.237 回答