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" />
4
  • 1
    Please ask a question, not just post code Commented Jan 29, 2011 at 14:56
  • @ El Ronnoco : In the above code there is always value false in alert box why so? Commented Jan 29, 2011 at 14:59
  • @Victo: That is what i am not understanding Commented Jan 29, 2011 at 15:16
  • try arguments.Value instead of arguments.value. Javascript is case-sensitive. Commented Jan 29, 2011 at 15:23

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer; Tried the code but still there is error. Thanks in advance
It works but why does this not work if i dont use controltovalidate?
@jagdeep, because the CustomValidator needs something to validate. ControlToValidate does just what it says, it validates a control that you specify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.