Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am trying to validate the check box using the following code.
But it is always return false.
//----------------------------------------
XML
<script  language="javascript" type ="text/javascript" >
       function checkAgreement(source, args)
        {
         var elem = document.getElementById("cbxPDCheque");
         if (elem.checked)
          {
          args.IsValid = true;
          }
          else
          {
          args.IsValid = false;
          }
        }
        </script>

//=====================================================
XML
<asp:CheckBox ID="cbxPDCheque" runat="server" AutoPostBack="True" Text="PD Cheques" />
  <asp:CustomValidator ID="CustomValidator1" runat="server" EnableClientScript ="true"
  ErrorMessage="Select PD Cheques."  ClientValidationFunction ="checkAgreement" >*</asp:CustomValidator>
  <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False" />
  <asp:Button ID="Button1" runat="server" Text="Button" />

//=======================================================
Posted
Comments
Hemant__Sharma 29-Apr-11 8:41am    
Hello SHAJANCHERIAN,
I tried your code and it worked perfectly fine at my end. i got the validation failure popup when validation fails, no issue otherwise.

when you say it always return false you mean the validation is coming every time right or you are using the checkAgreement function somewhere else to see the returned value bcz the checkagreement function doesn't return any value and default return value will be false.

do you have any additional code in your page?

Thanks,
Hemant
SHAJANCHERIAN 2-May-11 0:39am    
var elem = document.getElementById("<%=cbxPDCheque.ClientID%>");
I have made change as suggested by nit_singh1..Now it is working ..thank you

I reckon your ClientID is wrong.

var elem = document.getElementById("cbxPDCheque");



You need to view the source of your page to find out the correct ClientID of the CheckBox. The ID of a server Control is not necessarily the same as the DOM attribute ID that gets rendered in the browser.
 
Share this answer
 
Comments
SHAJANCHERIAN 2-May-11 0:43am    
Thank you
var elem = document.getElementById("<%=cbxPDCheque.ClientID%>"); thank you
var elem = document.getElementById("<%=cbxPDCheque.ClientID%>");

Because the ID of the controls is changed after rendering.
 
Share this answer
 
Comments
SHAJANCHERIAN 2-May-11 0:36am    
Thank You nit_singh1..
Its working
sachinvijwal 18-Sep-12 10:57am    
In Ajax Update panel, it's not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900