我有2个字段,并且只有其他字段中包含“NO”值的内容时,我需要将其中一个字段设置为必填字段。所以我在网上进行了研究,这就是我最终得到的结果。唯一的问题是我需要的帮助是代码的第一部分,它称之为:.getElementByID
。我怎样才能构建它?或者,如果你可以摆脱一些灯光将是伟大的。我如何根据另一个字段的输入验证字段
<script>
function Validate(source, args) {
if (document.getElementById('<%= TextBox1.ClientID %>').value.toUpperCase() == "NO" &&
document.getElementById('<%= TextBox2.ClientID %>').value.length == 0)
args.IsValid = false;
else
args.IsValid = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TextBox2 Should Be Filled"
Display="Dynamic" ClientValidationFunction="Validate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Page.Validate();
if (!Page.IsValid)
Response.Write("TextBox2 should be filled");
}
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = !(this.TextBox1.Text.Equals("NO", StringComparison.CurrentCultureIgnoreCase) && this.TextBox2.Text.Length == 0);
你在寻找服务器端验证? – BrOSs