客户端CustomValidator不会验证

时间:2011-10-12 18:57:11

标签: javascript asp.net 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 #&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>

2 个答案:

答案 0 :(得分:1)

只有TextBox不为空时才会触发“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> 

希望这有助于!!

答案 1 :(得分:0)

arguments.isValid更改为arguments.IsValid。它必须有一个大写“我”。

编辑:那么,你为什么要检查arguments.length?我想你想要:arguments.Value.length

查看此资源:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx