Search This Blog

Friday, December 11, 2009

ASP.Net Repeater: checkbox validation using javascript

Javascript function for validating checkbox:

The repeater control has two chechbox.
Following function will warn user if he selects second checkbox without selecting first checkbox.


function ValidateCB(cb2)
{

//Get the object for Second Check box (Control ID is passed in method : this.id)
var cbSecondID = document.getElementById(cb2);

//Contruct Control ID for First CheckBox, by replacing string (control id of second checkbox)
var cbFirstID = cbSecondID.id.replace("cb2","cb1");

//Get the object for first checkbox
var cbFirst = document.getElementById(cbFirstID);

//displays alert when first cb is not selected and we are attampting to select second one
if (cbSecondID.checked == true && cbFirst.checked == false)
{
alert("Please select first checkbox");
}

}

2 comments:

  1. Good... very informative.

    ReplyDelete
  2. How to use these js in code.. pls give me some ex.

    ReplyDelete

Comments