为什么不能在ASP.NET中使用JavaScript创建自定义验证器?

Why can’t I create a custom validator using JavaScript in ASP.NET?

本文关键字:创建 JavaScript 自定义 验证 不能 ASP NET 为什么      更新时间:2023-09-26

我有这段代码。它应该实现我的自定义验证,但它不起作用:

<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
</asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*Required"
ClientValidationFunction = "ValidateListBox"></asp:CustomValidator>
<script type = "text/javascript">
function ValidateListBox(sender, args) {
    var options = document.getElementById("<%=ListBox1.ClientID%>").options;
    if (options.length > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }       
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" />

我用了Firebug,但是没有结果

我已经修改了代码,似乎你的代码中有拼写错误,我已经编辑了它:)