Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate a checkbox?

Status
Not open for further replies.

BunBo900

MIS
Dec 11, 2009
50
US
Hello:

The code below is working fine, but when I incorporate the JavaScript function (checkAgreement) below into my existing pages with alot more code it makes certain code on my page not working.
I need to make the end user checks on the checkbox before moving to the next screen.
Is there another way to validate the checkbox like below that I can use? Thanks in advance.

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validateCheckBox2.aspx.cs" Inherits="validateCheckBox2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">      
        function checkAgreement(source, args)     
        {                         
            var elem = document.getElementById('<%= chkAgree.ClientID %>');
                     
                if (elem.checked)         
                {             
                    args.IsValid = true;         
                }         
                else         
                {   
                    args.IsValid = false;         
                }     
        }
    </script> 
    
</head>

<body>
    <form id="form1" runat="server">

    <div>
        <asp:CheckBox ID="chkAgree" runat="server" /> 
        <asp:Label ID="Label1" AssociatedControlID="chkAgree" runat="server">I agree to the term</asp:Label> 
        <br />  
        <asp:CustomValidator ID="chkAgreeValidator" runat="server" ClientValidationFunction="checkAgreement" ErrorMessage="You must agree to the terms and conditions."></asp:CustomValidator> 
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Enroll in this Course!" />
    </div>
    </form>
</body>
</html>
 
Meaning the current already complicated page is working fine with this Javascript function checkAgreement(source, args) above.

Now I need to add a new search capablity to this page. After hours of trying, found out that the above Javascript causing the new search function not to work. Without the above Javascript, the new added search function works with the rest of the page. But without the script I would lose the capability of forcing the end user to click on the check box before moving to the next screen. so I would like to rewrite the above script to make it work with the new search and the checkbox. I am posting part of the page here. Hope this clears up a little more...Thanks

<!-- aspx page -->

Code:
<script type="text/javascript">      
        function ensureChecked(source, args) {
            var cb = document.getElementById('<%=cbxAgreement.ClientID%>');

            if (cb.checked == true) {
                args.IsValid = true;

            } else {
                args.IsValid = false;
            }

        }    
</script> 


<ajax:UpdatePanel ID="updPanel" runat="server">
        <ContentTemplate>
                <asp:Panel ID="pnlSearch" runat="server">
                    <table style="width:70%" border="0">
                        <tr>
                            <td align="left" colspan="3">
                            Search by:<b> KeyWords</b> 
                            </td>
                           
                        </tr>
                        <tr>
                            <td style="width: 40%; " align="left">
                                Keywords:
                            </td>
                            <td align="left" style="width:30%; height: 26px;">
                                <asp:TextBox ID="txtKeywords" runat="server" Width="120px"></asp:TextBox>
                            </td>
                            <td style="width: 30%" align="right">
                                <asp:Button ID="btnKeywords" runat="server" OnClick="btnKeywords_OnClick" Text="Search" /><asp:Button ID="btnReset" Text="Reset" OnClick="btnReset_OnClick" runat="server" />
                            </td>
                        </tr>
                    </table>
                
                </asp:Panel>


<asp:Panel ID="pnlEnrollmentAgreement" runat="server" Width="400px" Height="300px"
                    Style="display: none">
                    <table>
                        <tr>
                            <td align="left" valign="top">
                                <asp:Label ID="lbAgreementHeader" runat="server" Width="100%" BackColor="#E2E3C2"
                                    ForeColor="Black" CssClass="linkStyle" Font-Size="14px" Text="<b>Step 3</b> - Enrollment Agreement"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td align="left">
                                <asp:CheckBox ID="cbxAgreement" runat="server" Text="I have received pre-enrollment counseling and will notify the appropriate training NCO or educational officer of this enrollment." /><br />
                                <br />
                                <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ensureChecked" ErrorMessage="* Please read then check the agreement to continue"></asp:CustomValidator>                              
                                <br />
                                <br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="btnSubmit" Visible="false" runat="server" Text="Enroll in this Course!" OnClick="btnSubmit_OnClick" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </asp:Panel>
 
You need to make sure the validator is in a GROUP that is separate from the SEARCH validation GROUP. Otherwise it is always running that validation.

Lodlaiden

You've got questions and source code. We want both!
Here at tek tips, we provide a hand up, not a hand out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top