1

In my aspx page i am having a checkbox list ..It has binded values from a table.. I need to validate the checkbox list ..I tried the following script

 var checkBoxCount = 0;     

        var elements = document.getElementById('<%=ChkBoxList.ClientID%>');

        for(i=0; i<elements.length;i++)

        {

        if(elements[i].checked) 

        checkBoxCount++;

        }  

        if (checkBoxCount == 0)
               {
                alert("Please choose atleast one");
              return false;
              }

But I can't get the required output, it requires to select all the values in the checkbox list ..My need is atleast only one item must be selected from the checkbox list.. Using javascript

Thanks in advance...

3
  • What do you mean you can't get output? Commented Oct 12, 2010 at 12:28
  • 2
    You can't use an "id" value more than once. In other words, the "id" of every element must be something unique. Commented Oct 12, 2010 at 12:29
  • To elaborate on Pointy's point, your script is probably erroring out at for(i=0; i<elements.length;i++), because elements is a single DOM element, which doesn't have a length property. In order to get the elements you're looking for, you'll have to use a different method. Commented Oct 12, 2010 at 12:34

4 Answers 4

1
function readListControl()
{
 var tableBody = document.getElementById('CheckBoxList1').childNodes[0];

 for (var i=0;i<tableBody.childNodes.length; i++)
 {
  var currentTd = tableBody.childNodes[i].childNodes[0];
  var listControl = currentTd.childNodes[0];

  if ( listControl.checked == true )
   alert('#' + i + ': is checked');
 }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ugh... it renders as tables? This is why I hate ASP.NET.
let me give you a little advice when writing javascript before writing anything just take a look at the source after the page is rendered it'll help you alot in writing the javascript.
Tnx for the reply.And tnx for KhanZeeshan for your kindly advice
no problem i was just sharing what i experienced, and please mark it as answer if the solution helped you. thanks
0

document.getElementById returns an element, not an array.

One way to do this would be to get the container and iterate through the inputs, like so:

var container = document.getElementById('<%=ChkBoxList.ClientID%>').parentNode;
var inputs = container.getElementsByTagName('input');

for (var i=0; i<inputs.length; i++) {
  if (typeof inputs[i] = "checkbox") {
    // statements
  }
}

You may also want to qualify the inputs with more conditional statements. This just gives you the broad brush strokes.

1 Comment

public void bind_chbox() { SqlDataAdapter cmd = new SqlDataAdapter("sp_bind_chbox", cn); cmd.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); cmd.Fill(ds); ChkBoxList.DataSource = ds; ChkBoxList.DataTextField = "CSE"; ChkBoxList.DataValueField ="ID"; ChkBoxList.DataBind(); } By using this function I bound value for the checkbox list..
0

You will have to show us your generated html.

However, here is a working example:

<html>
<body><form name="myform" method="POST" action="" onsubmit="return validate();">
<input type="checkbox" name="mybox" value="1" /> 1 
<input type="checkbox" name="mybox" value="2" /> 2 
<input type="checkbox" name="mybox" value="3" /> 3 
<input type="checkbox" name="mybox" value="4" /> 4 
<input type="checkbox" name="mybox" value="5" /> 5 
<input type="submit" value="Submit Form" />
</form>

<script type = "text/javascript">
function validate() {
    var checkBoxCount = 0;
    for (var i = 0; i< 5; i++) {
        if(document.myform["mybox"][i].checked){
            checkBoxCount ++;
        }
    }
    if (checkBoxCount == 0) {
        alert ("Tick a box!");
        return false;
    }
    return true;
}
</script>
</body>
</html>

1 Comment

public void bind_chbox() { SqlDataAdapter cmd = new SqlDataAdapter("sp_bind_chbox", cn); cmd.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); cmd.Fill(ds); ChkBoxList.DataSource = ds; ChkBoxList.DataTextField = "CSE"; ChkBoxList.DataValueField ="ID"; ChkBoxList.DataBind(); } By using this function I bound value for the checkbox list.. So only I can't able to put html codes..
0

var k=0; var ControlRef = document.getElementById('ChkBoxList'); var CheckBoxListArray = ControlRef.getElementsByTagName('input'); for (var i=0; i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.