以下是代码:
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语句在该行上停止。因此,该行上其余的字段未经过验证。
我有字段的名字,姓氏和性别。
如果我忘记添加名字和姓氏,而不是性别。
此验证只会显示我缺少名字,直到第一个名字修复后才会检查姓氏。
他们是否有办法解决这个问题,以便两个字段同时进行检查?
答案 0 :(得分: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();
}
答案 1 :(得分: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";
}