You can't have more than one element with the same ID, and
"AreasOfInterest[]" is not a valid ID. It looks like you were hoping that
the [] notation would give you an array of elements that have the same ID.
Use a common classname on your checkboxes instead, e.g.
<input type="checkbox" class="need7" id="first" />
<input type="checkbox" class="need7" id="second" />
<input type="checkbox" class="need7" id="third" />
...etc...
Then you can use:
$(function() {
$('form').submit( function() {
if( $('input.need7:checked').length < 7 ) {
alert( 'Please select at least 7 items' );
return false;
});
});
-Mike
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of [email protected]
> Sent: Saturday, May 02, 2009 4:46 PM
> To: jQuery (English)
> Subject: [jQuery] Minimum amount of checkbox selected validation
>
>
> Hello,
>
> Trying to use jquery to validate that a minimum of 7 check
> boxes are selected. the code is as follows:
>
> $(document).ready(function()
> {
> $("form").submit(function()
> {
> if (!isCheckedById("AreasOfInterest[]"))
> {
> alert ("Please select at least 7 items");
> return false;
> }
> else
> {
> return true; //submit the form
> }
> });
>
> function isCheckedById(id)
> {
> var checked = $("inp...@id="+id+"]:checked").length;
> if (checked < 7)
> {
> return false;
> }
> else
> {
> return true;
> }
> }
> });
>
> It works if i set the (checked == 0) and then at least 1 is
> selected... but when i run it as is... it won't submit even
> if more than 7 are selected... Is my code wrong?? is the
> variable checked not an integer?? help!!
>