views:

35

answers:

3

Using Asp.net Webforms how can I validate that a checkbox has been checked. (I cannot do a postback) has to be in javascript (either custom - working with the existing validation controls for other fields. or using a .net validation control)

Thanks

+1  A: 

in jquery

if ($('#checkBoxID').attr('checked'))
{
    //code
}

in normal javascript

if (getElementById('checkBoxID').checked)
{
    //code
}
Paul Creasey
This doesnt work with the other valdiation methods.
Pino
You can use this type of code in the client script of a custom validator
Ray
+2  A: 

You can use a CustomValidator control and specify an ClientValidationFunction javascript function.

Find more info here:
http://forums.aspfree.com/net-development-11/compare-validator-for-a-checkbox-121590.html

But if you're only doing client-side validation, the whole CustomValidator implementation might be a bit useless. Just validate the checkbox with javascript on the form submit.

Zyphrax
+1  A: 
if (document.getElementById("check_box_id").checked) {
  // it is checked
}
else {
  // it is not
}

Or with jQuery:

if ($(("#check_box_id").attr("checked") == true) {
  // it is checked
}
else {
  // it is not
}
Ray