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!

validation of asp.net checkbox 1

Status
Not open for further replies.

jordanking

Programmer
Sep 8, 2005
351
hello,

I have a checkbox in an asp form. I want to have client side validation to ensure it is checked before the form is submitted.

Here is the code in the form:
Code:
                <script type="text/javascript">
                    function CheckAgreement(source, args) {
                        var Agreement = document.getElementById('<%= cbAgreement.ClientID %>');
                        if (!Agreement.checked){
                            args.isValid = false;
                        }
                        else {
                            args.isValid = true;
                        }
                    }
                </script>
                
                <div class="Form_Note">
                    <asp:CustomValidator ID="cvAgreement" runat="server" 
                        ErrorMessage="You must agree to the Terms and Conditions" Text="*" 
                        SetFocusOnError="True" ClientValidationFunction="CheckAgreement"></asp:CustomValidator>
                    <asp:CheckBox ID="cbAgreement" runat="server" />
                    By checking this box I agree to the terms and conditions ...
                </div>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit Request" CssClass="floatRight" 
                    Font-Bold="True" Font-Size="Medium" />

The page submits without checking or throwing an error. Can anyone help me?


.....
I'd rather be surfing
 
doesn't a requiredfieldvalidator work?
if not then you are on the right path. IIRC you can set the control to validate to the check box and reference it in the js. you can also simplify your client validation. finally, js is case sensative so 'isValid' should be 'IsValid'.
JavaScript:
function CheckAgreement(source, args) {
   args.IsValid = arg.Value;
}
Code:
<asp:CustomValidator ... ClientValidationFunction="CheckAgreement" ControlToValidate="cbAgreement" />
<asp:CheckBox ID="cbAgreement" runat="server" />
if the ControlToValidate doesn't work with checkboxes, then you will have to go with hardcoding the control id as before.

another alternative is to use jquery validation on the client and domain rules on the server.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
thanks jmeckly,

changing isValid to the case sensitive IsValid fixed the problem. Works great.

By the way, you cannot set a "control to validate" to a checkbox with asp built in validation tools. You have to use a custom validator with that field blank.

cheers

.....
I'd rather be surfing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top