简体   繁体   English

客户端CustomValidator无法验证

[英]Client-side CustomValidator won't validate

Why won't this client-side CustomValidator validate? 为什么此客户端CustomValidator无法验证? When the webpage(1) is run, the CustomValidator ignores the validation rules and doesn't diplay an appropriate message in ValidationSummary section. 当运行webpage(1)时,CustomValidator会忽略验证规则,并且不会在ValidationSummary部分中显示适当的消息。

The validator must issue an error when txtTotalCost is empty. 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>

The "CustomValidator" will fire only when the TextBox is not empty. 仅当TextBox不为空时,才会触发“ CustomValidator”。 If you want to check whether it is empty or not, use the "RequiredField Validator" along with the "CustomValidator". 如果要检查它是否为空,请使用“ RequiredField Validator”和“ CustomValidator”。 Also, the "IsValid" fix suggested by "Brian" is absolutely correct. 另外,“ Brian”建议的“ IsValid”修复程序是绝对正确的。

Edit1: 编辑1:

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 刚才检查了一下,我们可以对自定义验证器使用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> 

Hope this Helps!! 希望这可以帮助!!

Change arguments.isValid to arguments.IsValid . 更改arguments.isValidarguments.IsValid It has to have a capital "I". 它必须有一个大写的“ I”。

EDIT: ALso, why are you checking arguments.length? 编辑:另外,为什么还要检查arguments.length? I think you want: arguments.Value.length ? 我认为您想要: arguments.Value.length

Check out this resource: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx 签出此资源: http : //msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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