最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why ClientValidationFunction function is not working properly? - Stack Overflow

programmeradmin1浏览0评论
<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" />
<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" />
Share Improve this question edited Jan 29, 2011 at 15:08 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Jan 29, 2011 at 14:56 LearnerLearner 1,6448 gold badges32 silver badges58 bronze badges 4
  • 1 Please ask a question, not just post code – El Ronnoco Commented Jan 29, 2011 at 14:56
  • @ El Ronnoco : In the above code there is always value false in alert box why so? – Learner Commented Jan 29, 2011 at 14:59
  • @Victo: That is what i am not understanding – Learner Commented Jan 29, 2011 at 15:16
  • try arguments.Value instead of arguments.value. Javascript is case-sensitive. – El Ronnoco Commented Jan 29, 2011 at 15:23
Add a ment  | 

1 Answer 1

Reset to default 4

Try this,

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

EDIT: Set ControlToValidate property.

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

评论列表(0)

  1. 暂无评论