客户端 CustomValidator 不会验证
为什么这个客户端 CustomValidator 无法验证?运行网页 (1) 时,CustomValidator 会忽略验证规则,并且不会在 ValidationSummary 部分中显示适当的消息。
当 txtTotalCost 为空时,验证器必须发出错误。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script language="javascript" type="text/javascript">
function ValidateTotalCost(source, arguments) {
if (arguments.length <= 0) {
arguments.isValid = false;
}
else {
arguments.isValid = true;
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
Order #
<asp:TextBox ID="txtOrderNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validateOrderNumber" runat="server"
ControlToValidate="txtOrderNumber" ErrorMessage="Please enter order number"
ToolTip="Please enter order nunmber">*</asp:RequiredFieldValidator>
<br />
<br />
Item ID
<asp:TextBox ID="txtItemID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validateItemID" runat="server"
ControlToValidate="txtItemID" ErrorMessage="Please enter item ID"
ToolTip="Please enter item ID">*</asp:RequiredFieldValidator>
<br />
<br />
Qty
<asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
<asp:RangeValidator ID="validateQty" runat="server" ControlToValidate="txtQty"
ErrorMessage="Qty value range must be between 0 - 50" MaximumValue="50"
MinimumValue="0" ToolTip="Qty value range must be between 0 - 50">*</asp:RangeValidator>
<br />
<br />
Last Qty
<asp:TextBox ID="txtLastQty" runat="server" style="margin-left: 0px"></asp:TextBox>
<asp:CompareValidator ID="validateLastQty" runat="server"
ControlToCompare="txtQty" ControlToValidate="txtLastQty"
ErrorMessage="Qty and LastQty must match" ToolTip="Qty and LastQty must match">*</asp:CompareValidator>
<br />
<br />
Total
<asp:TextBox ID="txtTotalCost" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="txtTotalCost"
ErrorMessage="Total cost must be filled out"
ClientValidationFunction="ValidateTotalCost"
ToolTip="Total cost must be filled out">*</asp:CustomValidator>
<br />
<br />
<asp:Button ID="btnOK" runat="server" Text="OK" />
<br />
</div>
</form>
</body>
</html>
Why won't this client-side CustomValidator validate? When the webpage(1) is run, the CustomValidator ignores the validation rules and doesn't diplay an appropriate message in ValidationSummary section.
The validator must issue an error when txtTotalCost is empty.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script language="javascript" type="text/javascript">
function ValidateTotalCost(source, arguments) {
if (arguments.length <= 0) {
arguments.isValid = false;
}
else {
arguments.isValid = true;
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
Order #
<asp:TextBox ID="txtOrderNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validateOrderNumber" runat="server"
ControlToValidate="txtOrderNumber" ErrorMessage="Please enter order number"
ToolTip="Please enter order nunmber">*</asp:RequiredFieldValidator>
<br />
<br />
Item ID
<asp:TextBox ID="txtItemID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validateItemID" runat="server"
ControlToValidate="txtItemID" ErrorMessage="Please enter item ID"
ToolTip="Please enter item ID">*</asp:RequiredFieldValidator>
<br />
<br />
Qty
<asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
<asp:RangeValidator ID="validateQty" runat="server" ControlToValidate="txtQty"
ErrorMessage="Qty value range must be between 0 - 50" MaximumValue="50"
MinimumValue="0" ToolTip="Qty value range must be between 0 - 50">*</asp:RangeValidator>
<br />
<br />
Last Qty
<asp:TextBox ID="txtLastQty" runat="server" style="margin-left: 0px"></asp:TextBox>
<asp:CompareValidator ID="validateLastQty" runat="server"
ControlToCompare="txtQty" ControlToValidate="txtLastQty"
ErrorMessage="Qty and LastQty must match" ToolTip="Qty and LastQty must match">*</asp:CompareValidator>
<br />
<br />
Total
<asp:TextBox ID="txtTotalCost" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="txtTotalCost"
ErrorMessage="Total cost must be filled out"
ClientValidationFunction="ValidateTotalCost"
ToolTip="Total cost must be filled out">*</asp:CustomValidator>
<br />
<br />
<asp:Button ID="btnOK" runat="server" Text="OK" />
<br />
</div>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当文本框不为空时,“CustomValidator”才会触发。如果您想检查它是否为空,请使用“RequiredField Validator”和“CustomValidator”。此外,“Brian”建议的“IsValid”修复是绝对正确的。
Edit1:
刚刚检查过,我们可以对自定义验证器使用 ValidateEmptyText="true" 属性,以避免使用必需的字段验证器,如下所示
希望这有帮助!
The "CustomValidator" will fire only when the TextBox is not empty. If you want to check whether it is empty or not, use the "RequiredField Validator" along with the "CustomValidator". Also, the "IsValid" fix suggested by "Brian" is absolutely correct.
Edit1:
Just now checked that, we can use ValidateEmptyText="true" attribute for the custom validator in order to avoid the required field validator something like below
Hope this Helps!!
将
arguments.isValid
更改为arguments.IsValid
。它必须有一个大写的“I”。编辑:另外,你为什么要检查arguments.length?我想你想要:
arguments.Value.length
?查看此资源: http:// msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx
Change
arguments.isValid
toarguments.IsValid
. It has to have a capital "I".EDIT: ALso, why are you checking arguments.length? I think you want:
arguments.Value.length
?Check out this resource: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx