Validating a checkbox using javascript

The Check Box Validator is a custom validation control I created and shared in the Creating Validator Controls for the Check Box and Check Box List article. In this article we looked at how to extend the Check Box Validator control to include the ability to enable or disable a corresponding Button, Link Button, or Image Button control, based on the corresponding Check Box's checked state.

To use the control, you simply add it to a page and type in the property.

The Check Box Validator injects client-side script into the page to perform client-side validation in addition to its server-side validation.

By default, the Check Box Validator will only report that the data is valid if its Check Box is checked. In order to update the Check Box Validator to disable an associated Button control when invalid, we need to add a public property to the Check Box Validator class that enables the page developer to optionally specify the associated button control. Get Type(), "skm Validators.js")); // If there's an associated Button for this validator, add the script to enable/disable // it when the checkbox is clicked AND when the page is first loaded if (this. = null) { string call Check Box Validator Disable Button = string.

NET provides a variety of validation Web controls that can be used to validate a user's form field inputs.

Unfortunately, the validation Web controls do not work with the Check Box or Check Box List Web controls.

In a previous article, Creating Validator Controls for the Check Box and Check Box List, I shared two custom controls I had created to address this shortcoming: Check Box Validator and Check Box List Validator. NET validation control - at a minimum, just add them to a page and specify their properties.

A colleague recently asked me to enhance the Check Box Validator control's client-side script so that it could optionally disable a particular button when invalid.

In particular, she wanted to be able to specify the of a Button control in the Check Box Validator's properties.

The Check Box Validator would then use client script to disable (gray out) the Button when the Check Box Validator was invalid. To String(), call Check Box Validator Disable Button, true); } } } That's all there is to it!

The following screen shot illustrates this concept. The download at the end of this article includes the complete source code to the Check Box Validator and Check Box List Validator controls, along with a simple ASP.

On the left, the Check Box is unchecked so the Submit button is grayed out; on the right, the Check Box has been checked so the Button has become enabled. NET demo website showing how to use these two validation controls (including an example illustrating the property).