Manoj Singh
Manoj Singh

Reputation: 7707

How can I determine whether any checkbox is checked?

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

Upvotes: 0

Views: 312

Answers (4)

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

I advice use jQuery Validation plugin. It is very simple to use and so elegant (nice way to display error messages). Check that example. (There are two radio buttons which one should be checked.)

Upvotes: 0

aherrick
aherrick

Reputation: 20161

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!");

});

Upvotes: 1

ranonE
ranonE

Reputation: 497

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

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827178

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
}

Upvotes: 3

Related Questions