Enable/Disable ASP.Net Jquery 需要验证器
Enable/Disable ASP.Net Required Validator with Jquery
我在 .aspx 页面中有多个 Select 下拉列表。
<asp:DropDownList ID="ddlProduct" runat="server" CssClass="form-control ui fluid dropdown" multiple="">
</asp:DropDownList>
我的要求是我 select 从下拉列表中选择的产品(它是从数据库中填充的)它应该显示一个产品文本框,您应该在其中输入总数量。这是文本框的代码
<label class="control-label">Total Quantity Purchased</label>
<div class="col-md-12 hideshow1" >
<asp:TextBox ID="Product1Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="1Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product1Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product1Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
<div class="col-md-12 hideshow3">
<asp:TextBox ID="Product2Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="2Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product2Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product2Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
<div class="col-md-12 hideshow4">
<asp:TextBox ID="Product3Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="3Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product3Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product3Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
我正在使用 Jquery 到 Show/Hide 文本框。我想要的是只应为 selected 产品启用 RequiredFieldValidator。
<script>
$('.ui.fluid.dropdown').dropdown();
$('.ui.fluid.dropdown').change(function () {
$(".hideshow1").hide();
ValidatorEnabled($("<%=RequiredFieldValidator1%>"), false);
$(".hideshow3").hide();
ValidatorEnabled($("<%=RequiredFieldValidator3%>"), false);
$(".hideshow4").hide();
ValidatorEnabled($("<%=RequiredFieldValidator4%>"), false);
$(".ui.fluid.dropdown option:selected").each(function () {
var selection = "";
selection = ".hideshow" + $(this).val();
$(selection).show();
ValidatorEnabled($("<%=RequiredFieldValidator1%>"), true);
});
});
</script>
我在控制台中收到此错误
"Uncaught TypeError: Cannot set property 'visibility' of undefined(…)".
默认情况下,所有文本框 Div 都是 "display: none"。
这是我的主页。
我推荐使用 CustomValidators
。以下验证器仅在父 <div>
显示为 block
且文本框为空时才会触发。
<div style="display: none">
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myGroup"></asp:TextBox>
</div>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ValidateEmptyText="true" ErrorMessage="Value is required" ValidationGroup="myGroup" ClientValidationFunction="myCustomValidation"></asp:CustomValidator>
<script type="text/javascript">
function myCustomValidation(oSrc, args) {
if (document.getElementById(oSrc.controltovalidate).parentElement.style.display == "block" && args.Value == "") {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
</script>
在 <%= 前面使用“#”对我有用
ValidatorEnabled($("#<%=RequiredFieldValidator1%>"), false);
我在 .aspx 页面中有多个 Select 下拉列表。
<asp:DropDownList ID="ddlProduct" runat="server" CssClass="form-control ui fluid dropdown" multiple="">
</asp:DropDownList>
我的要求是我 select 从下拉列表中选择的产品(它是从数据库中填充的)它应该显示一个产品文本框,您应该在其中输入总数量。这是文本框的代码
<label class="control-label">Total Quantity Purchased</label>
<div class="col-md-12 hideshow1" >
<asp:TextBox ID="Product1Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="1Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product1Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product1Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
<div class="col-md-12 hideshow3">
<asp:TextBox ID="Product2Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="2Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product2Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product2Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
<div class="col-md-12 hideshow4">
<asp:TextBox ID="Product3Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="3Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product3Textbox"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product3Textbox"
ValidationExpression="\d+"></asp:RegularExpressionValidator>
</div>
我正在使用 Jquery 到 Show/Hide 文本框。我想要的是只应为 selected 产品启用 RequiredFieldValidator。
<script>
$('.ui.fluid.dropdown').dropdown();
$('.ui.fluid.dropdown').change(function () {
$(".hideshow1").hide();
ValidatorEnabled($("<%=RequiredFieldValidator1%>"), false);
$(".hideshow3").hide();
ValidatorEnabled($("<%=RequiredFieldValidator3%>"), false);
$(".hideshow4").hide();
ValidatorEnabled($("<%=RequiredFieldValidator4%>"), false);
$(".ui.fluid.dropdown option:selected").each(function () {
var selection = "";
selection = ".hideshow" + $(this).val();
$(selection).show();
ValidatorEnabled($("<%=RequiredFieldValidator1%>"), true);
});
});
</script>
我在控制台中收到此错误
"Uncaught TypeError: Cannot set property 'visibility' of undefined(…)".
默认情况下,所有文本框 Div 都是 "display: none"。
这是我的主页。
我推荐使用 CustomValidators
。以下验证器仅在父 <div>
显示为 block
且文本框为空时才会触发。
<div style="display: none">
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myGroup"></asp:TextBox>
</div>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ValidateEmptyText="true" ErrorMessage="Value is required" ValidationGroup="myGroup" ClientValidationFunction="myCustomValidation"></asp:CustomValidator>
<script type="text/javascript">
function myCustomValidation(oSrc, args) {
if (document.getElementById(oSrc.controltovalidate).parentElement.style.display == "block" && args.Value == "") {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
</script>
在 <%= 前面使用“#”对我有用
ValidatorEnabled($("#<%=RequiredFieldValidator1%>"), false);