C# 在处理值之前,如何在c代码中验证表单
我想在提交给服务器之前验证一个表单,该表单包含6个文本框和一个下拉框。我的要求是在5个文本框中,任何一个都足够,它们是C# 在处理值之前,如何在c代码中验证表单,c#,asp.net,.net,validation,C#,Asp.net,.net,Validation,我想在提交给服务器之前验证一个表单,该表单包含6个文本框和一个下拉框。我的要求是在5个文本框中,任何一个都足够,它们是txtid.text,txtAccountNo.text,txtPlot.text,txtLongitude.text(X),txtylegitude.text(Y)。这里txtLongitude.text(X),txtyleQuantity.Text(Y)两者都需要在一起。如果没有给出任何一个,并且dropdonwboxdrpBranch仅被选中,则需要一个文本框txtPlot
txtid.text
,txtAccountNo.text
,txtPlot.text
,txtLongitude.text(X),txtylegitude.text(Y)
。这里txtLongitude.text(X),txtyleQuantity.Text(Y)
两者都需要在一起。如果没有给出任何一个,并且dropdonwboxdrpBranch
仅被选中,则需要一个文本框txtPlot
与dropdownbox一起使用。如果上面给出的和txtBlock
文本框都没有值,则需要txtPlot
与txtBlock
一起使用。
我所做的如下所示。但代码中的问题是如果drpBranch
或
txtBlock
和前5个文本框中的任何一个都有值,那么无需等待txtPlot.text
。如何实现此逻辑
private bool ValidateControls()
{
lblMessage.Text = "";
if ((!string.IsNullOrEmpty(drpBranch.SelectedValue)) && (string.IsNullOrEmpty(txtPlot.Text)))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
if ((!string.IsNullOrEmpty(txtBlock.Text)) && (string.IsNullOrEmpty(txtPlot.Text)))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
//else
if ((string.IsNullOrEmpty(txtEid.Text)) && (string.IsNullOrEmpty(txtAccountNo.Text)) && (string.IsNullOrEmpty(txtPlot.Text)) && ((string.IsNullOrEmpty(txtLongitude.Text)) &&
(string.IsNullOrEmpty(txtYlatitude.Text))))
{
lblMessage.Text = "Please enter either EID or Account Number or Plot Number or X,Y co ordinates to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
return true;
}
//创建字符串列表
//将所有5个文本框值添加到列表中
//创建一个布尔变量以检查所有文本框是否为空
//循环列表
//如果atlist one txtValue不为空,请退出循环并将bool value设置为false
List txtValues=新列表();
//添加所有5个文本框
添加(txtBlock.Text);
bool allEmpty=true;
foreach(txtValues中的字符串txtValue){
如果(!string.IsNullOrEmpty(txtValue)){
allEmpty=false;
//循环退出
}
}
if(allEmpty&!string.IsNullOrEmpty(drpBranch.SelectedValue)){
//检查txtPlot值
}
注意:显然这还不完整,我正在努力弄清楚…
1.txtEid.Text
2.txtAccountNo.Text
3.txtPlot.Text
4.txtLongitude.Text(X) (require if 5)
5.txtYlatitude.Text(Y) (require if 4)
6. dropdown ("Please select" = -1)
7. dropdownText
if(dropdown != -1) Ie they choose to use the dropdown.
{
if(dropdownText != "")
{
}
else
{
lblMessage.Text = "Please enter the dropdownText";
lblMessage.ForeColor = Color.Red;
return false;
}
}
//validate both field where given..if the other was.
//there is a smarter way to write this...
if(txtLongitude != "" || txtYlatitude != "")
{
if(txtLongitude == "" && txtYlatitude == "")
{
lblMessage.Text = "Please give both Longitude and latitude";
lblMessage.ForeColor = Color.Red;
return false;
}
}
//your wording got confusing..., please update and ill update the answer.
按照你的措辞:
问题很简单,5个文本框值中的任何一个都足以处理
形式。如果没有给出,那么我们必须使用txtplot
对于下拉或txtblock条目
你现在能明白为什么我会感到困惑吗?我用了更简单的方法
private bool ValidateControls()
{
lblMessage.Text = "";
if (string.IsNullOrEmpty(txtEid.Text)
&& string.IsNullOrEmpty(txtAccountNo.Text)
&& string.IsNullOrEmpty(txtPlot.Text)
&& string.IsNullOrEmpty(txtLongitude.Text)
&& string.IsNullOrEmpty(txtYlatitude.Text))
{
if (!string.IsNullOrEmpty(drpBranch.SelectedValue)
&& string.IsNullOrEmpty(txtPlot.Text))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
if (!string.IsNullOrEmpty(txtBlock.Text)
&& string.IsNullOrEmpty(txtPlot.Text))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
else
{
lblMessage.Text = @"Please enter either EID or Account Number
or Plot Number or X,Y co ordinates
to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
}
return true;
}
您能重新表述一下吗:
但该代码中的问题是,如果drpBranch或txtBlock以及前5个文本框中的任何一个都有值,则无需等待txtPlot.text。如何实现此逻辑?
。您不在客户端进行验证有什么原因吗?@ShareYourKnowledge正如我所说的,5个文本框中的任何一个都足以处理表单,因此,某些主体选择下拉值,则无需等待txtPlot.Text。但是,如果5个文本框的值均未给出,则需要txtPlot.Text来执行此过程。如何将所有5个文本框的文本添加到数组或列表中。然后在列表中循环查找NOTNULL并实现logic@ShareYourKnowledge下面的答案显然不完整,但它应该告诉你如何检查所有5个文本框中的空值,然后才决定是否需要检查txtPlot文本框dropdownbox值呢?如果5个文本框为空,选择了dropdownbox,然后选中txtPlot。我已经发布了我的答案问题很简单。5个文本框值中的任何一个都足以处理表单。如果没有给出任何文本框值,则我们必须使用dropdown或txtblock entries所需的txtPlot。我已经发布了我的答案
if(txtEid.Text != ""
&& txtAccountNo.Text != ""
&& txtPlot.Text != ""
&& txtLongitude.Text != ""
&& txtYlatitude.Text != "")
{
//form is valid
}
else
{
//go with txtplot required either dropdown or txtblock entries
}
private bool ValidateControls()
{
lblMessage.Text = "";
if (string.IsNullOrEmpty(txtEid.Text)
&& string.IsNullOrEmpty(txtAccountNo.Text)
&& string.IsNullOrEmpty(txtPlot.Text)
&& string.IsNullOrEmpty(txtLongitude.Text)
&& string.IsNullOrEmpty(txtYlatitude.Text))
{
if (!string.IsNullOrEmpty(drpBranch.SelectedValue)
&& string.IsNullOrEmpty(txtPlot.Text))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
if (!string.IsNullOrEmpty(txtBlock.Text)
&& string.IsNullOrEmpty(txtPlot.Text))
{
lblMessage.Text = "Please enter Plot number to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
else
{
lblMessage.Text = @"Please enter either EID or Account Number
or Plot Number or X,Y co ordinates
to get the Network details";
lblMessage.ForeColor = Color.Red;
return false;
}
}
return true;
}