简体   繁体   中英

Client-side CustomValidator won't validate

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 #&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>

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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM