javascript 为什么 ClientValidationFunction 函数不能正常工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4837512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:03:11  来源:igfitidea点击:

Why ClientValidationFunction function is not working properly?

javascriptasp.netvalidationasp.net-mvc-validation

提问by Learner

<script type="text/javascript">
    function clientValidation(sender, arguments)
    {
        if (arguments.value == "hello world")
            arguments.isvalid = true;
        else
            arguments.isvalid = false;

        alert(arguments.isvalid);
    }
</script>

<asp:Label ID="lblName" runat="server" Text="Enter Your Name" />
<asp:TextBox ID="txtbxName" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="You are Not allowed" Display="None" ClientValidationFunction="clientValidation" ValidationGroup="ValidationSummary1" />
<br />
<asp:Label ID="lblClass" runat="server" Text="Class" />
<asp:TextBox ID="txtClass" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter Clas" ControlToValidate="txtClass" Display="None" ValidationGroup="ValidationSummary1" />
<br />            
<asp:ValidationSummary ValidationGroup="ValidationSummary1" ID="ValidationSummary1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Validate" ValidationGroup="ValidationSummary1" />

回答by adatapost

Try this,

试试这个,

function clientValidation(sender, arguments)
{
  if (arguments.Value == "hello world")
     arguments.IsValid = true;
  else
     arguments.IsValid = false;
}

EDIT: Set ControlToValidate property.

编辑:设置 ControlToValidate 属性。

<asp:CustomValidator ID="CustomValidator1" runat="server" 
         ErrorMessage="You are Not allowed" 
         ClientValidationFunction="clientValidation" 
         ValidationGroup="ValidationSummary1" 
         ControlToValidate="txtbxName">
</asp:CustomValidator>