tags:

views:

723

answers:

6

I am using checkboxes whose values is coming from database. Its name is same but name is fetching like:

<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>">
<%=bs.getServiceDesc()%>

through this i am getting the values from the database. Now i have to validate that at least one checkbox should be selected..

If any one can help me i shall be thankful to u. If i am giving like this the javascript code:

var services = document.getElementsById( 'chkBankServices' );
if(!(services[0].checked) && 
   !(services[1].checked) && 
   !(services[2].checked) && 
   !(services[3].checked) && 
   !(services[4].checked) && 
   !(services[5].checked) && 
   !(services[6].checked) &&
   !(services[7].checked) && 
   !(services[8].checked))
{ 
    alert( "Please select at least one service to submit." );
    return false;
}

It's not giving any alert message. Is anything wrong in that. Plz help me... Thanks in advance

A: 

Have you checked the rendered source to make sure your checkboxes are being given the expected names?

ck
+3  A: 

Try this:

var services = document.getElementById( 'chkBankServices' );
var checkboxes = services.getElementsByTagName('input');
var checked = false;
for (var i=0,i0=checkboxes.length;i<i0;i++)
if (checkboxes[i].type.toLowerCase()=="checkbox")
{
    if (checkboxes[i].checked) checked = true;
}

and then:

if (!checked)
{
    alert('Not checked');
    return false;
}
Wilq32
did you forget to add a test to see if the checkbox is checked? looks like your setting checked to true as soon as you find a checkbox input control
Christian Hagelid
oh well yes :D omg :((
Wilq32
thnks wilq.but tell me that what else i have to add in my html code.this is my html code<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>"><%=bs.getServiceDesc()%>
where i have to add TagName.plz help me
You dont have to name checkboxes at all - this solution I gave you just checks for any checkboxes that are inside chkBankServices element.
Wilq32
means i dont have to add anything in this<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>"><%=bs.getServiceDesc()%> but i use ur code it is not validating..means if i dont check on any checkbox and click on submit button ,it is submitting
Yes - mine code just sets flag "checked" - I added some lines for you there
Wilq32
but wilq its not working.its not rising any flag.like i am giving like this
function validate(){var services = document.getElementsById( 'chkBankServices' ); var checkboxes = services.getElementsByTagName('input'); var checked = false; for (var i=0,i0=checkboxes.length;i<i0;i++) if (checkboxes[i].nodeName.toLowerCase()=="checkbox") { if (checkboxes[i].checked) checked = true; } if (!checked) { alert('Not checked'); return false; }
else{ document.frmCustomerUpdation.cmd.value='btnUpdate'; document.frmCustomerUpdation.submit(); } }and HTML is***********<td colspan="2"><input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>" CHECKED><%= bs.getServiceDesc()%>
make sure that you use the version that I have right now, look out for document.getElementById and that there should be (.type) instead of (.nodeName)
Wilq32
+4  A: 

There is no getElementsById method, since there should only be one element with a given id. Perhaps you meant to use getElementsByName? This allows multiple elements to be returned.

As this is really a client side issue, it would help if you could post a sample of the generated HTML, and we can guide you further.

Paul Dixon
ok but how i can use getElementsByName becoz in my html code name is having <input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>"><%=bs.getServiceDesc()%>
@remish: Pay attention to the HTML the browser sees, not the template the server uses to generate it.
Anonymous
agreed - this is a client side issue, so it would help if you could post the HTML you are generating.
Paul Dixon
+3  A: 

in jQuery :

   alert(  $("#chkBankServices input[type=checkbox]:checked").length > 0 );
Brian Hedler
a thing of beauty
Christian Hagelid
Just test for the first element e.g. if ($('selector')[0])
J-P
A: 

I don't know if this will help you with your specific problem, but your code would be easier to read if you avoided the massive if block and used a loop instead:

var checked = false;
for(var i=0;i<services.length;i++){
  if(services[i].checked){
    checked = true;
  }
}
if(!checked){
    alert( "Please select at least one service to submit." );
    return false;
}
apphacker
wat the services will be taken here.if my html code is like this<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>"><%=bs.getServiceDesc()%>
A: 

Looking at your code, I'm betting you have that checkbox in a repeater of some sort and are creating multiple checkboxes with the same ID which is invalid html. I would wrap it in a div/span or something with an id like below:

 if (!isSomethingChecked()) {
    alert( "Please select at least one service to submit." );
    return false;
 }

  function isSomethingChecked() {
    var parent = document.getElementById("chkBankServices");
    for (var child in parent.childNodes) {
        var node = parent.childNodes[child];
        if (node && node.tagName === "INPUT" && node.checked) {
            return true;
        }
    }
    return false;
 }

I assumed the HTML looks like :

<div id="chkBankServices">
    <input type="checkbox" id="Checkbox1" />
    <input type="checkbox" id="Checkbox2" checked="checked"/>
    <input type="checkbox" id="Checkbox3" checked="checked"/>
    <input type="checkbox" id="Checkbox4" />
    <input type="checkbox" id="Checkbox5" />
    <input type="checkbox" id="Checkbox6" />
    <input type="checkbox" id="Checkbox7" />
</div>
Chad Grant
hi chad..i want to ask you something that how we can mention id as 1,2,and so on.coz my services is comming from database and i dont know how many services are available there.so tell me what i can do for this..
like in html we have given like this:<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>"><%=bs.getServiceDesc()%>and in javascriptvar services = document.getElementsById( 'chkBankServices' );if(!(services[0].checked) return false;}so how i match
I don't know what that mess of code is, but I think my code is self explanatory. You are not giving enough information for people to help you.
Chad Grant
ok i m giving you the whole code after that u tell what i have to do in dat so that i can check means alert msg should display if not a single checkbox is selected..
This is the HTML code***************************<input type="checkbox" id="chkBankServices" name="<%=bs.getServiceID()%>" CHECKED><%= bs.getServiceDesc()%>
*************Java Script cod***************var services = document.getElementsById( 'chkBankServices' ); if(!(services[0].checked) return false; }
else{ document.frmCustomerUpdation.cmd.value='btnUpdate'; document.frmCustomerUpdation.submit(); } }Now tell wat i have to add in HTML code and what in javascriptplz help me
Your html does not match your javascript, you have 9 checkboxes you are checking if they are checked, but your html only shows 1. Is it inside a <asp:Repeater> or something?
Chad Grant