tags:

views:

150

answers:

4

hi,

I have some checkboxes in vb.net code.

<tr>
    <td colspan="2">
     <asp:CheckBox ID="chkbxCreateAmendOrg" runat="server" Checked="False" Text="Create/Amend Organisation" />
    </td>
    <td colspan="2">
     <asp:CheckBox ID="chkbxCreateAmendCPUser" runat="server" Checked="False" Text="Create/Amend CP User" />
    </td>
</tr>
<tr>
    <td colspan="2">
     <asp:CheckBox ID="chkbxDeleteOrg" runat="server" Checked="False" Text="Delete Organisation" />
    </td>
    <td colspan="2">
     <asp:CheckBox ID="chkbxDeleteCPUser" runat="server" Checked="False" Text="Delete CP User" />
    </td>
</tr>

I want to give alert to user if they have not selected atleast one. Can i have jquery code for this

A: 

i advice use jQuery Validation plugin: http://plugins.jquery.com/project/validate . It is very simple to use and so elegant (nice way to display error massage). Check that example: http://jquery.bassistance.de/validate/demo/milk/ (there is 2 radio button which one should be checked)

Rin
+3  A: 

You can select all the not checked checkboxes and check the length or size() of the jQuery object:

if ($('input:checkbox:not(:checked)').length > 0) {
  // some checkboxes not checked
}
CMS
But asp.net wraps checkboxes with span tag for css style. if ($('span > input ...
Barbaros Alp
Yes, but this selector will look for all the checkboxes on the document.
CMS
it is not working for me can you please have a look
MKS
it not checking and giving alert everytime whether check boxes are checked or not
MKS
it is checking all the checkboxes should be checked, but i want that user should check atleast one of them
MKS
Yes, you are right CMS
Barbaros Alp
+1  A: 

Something like this should get it done...

$(document).ready(function() {

// get all checked
var checkboxes = $("input:checkbox:checked");
if(checkboxes.size() == 0)
   alert("Please mark a checkbox!");

});
aherrick
By accessing `checkboxes.attr("checked")` you are getting the *checked* attribute only of the first matched element...
CMS
A: 

The following code print an alert for each checkbox not flagged:

$("input:not(:checked)").each(function(){  
  alert( $(this).attr("id") + " isn't checked!" );
});

See also the selectors

ranonE