客户端 CustomValidator 不会验证

发布于 2024-12-09 15:42:41 字数 3412 浏览 0 评论 0原文

为什么这个客户端 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 #&nbsp;&nbsp;&nbsp;
        <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&nbsp;&nbsp;&nbsp; 
        <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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <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&nbsp; 
        <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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2024-12-16 15:42:41

仅当文本框不为空时,“CustomValidator”才会触发。如果您想检查它是否为空,请使用“RequiredField Validator”和“CustomValidator”。此外,“Brian”建议的“IsValid”修复是绝对正确的。

Edit1:

刚刚检查过,我们可以对自定义验证器使用 ValidateEmptyText="true" 属性,以避免使用必需的字段验证器,如下所示

    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtTotalCost" ValidateEmptyText="true"
        ErrorMessage="Total cost must be filled out" ClientValidationFunction="ValidateTotalCost"
        ToolTip="Total cost must be filled out">*</asp:CustomValidator> 

希望这有帮助!

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

    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtTotalCost" ValidateEmptyText="true"
        ErrorMessage="Total cost must be filled out" ClientValidationFunction="ValidateTotalCost"
        ToolTip="Total cost must be filled out">*</asp:CustomValidator> 

Hope this Helps!!

神妖 2024-12-16 15:42:41

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 to arguments.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文