tags:

views:

28

answers:

1

Is there a way to validate this code I have below? I mean I have a save button too where when I click save and none of the checkboxes are checked, I would like it to validate, but not validate where it throws an alert.

var chkbox= $('#divListBox').find(':checked')
                     .map(function() {
                         return $(this).val();
                     }).get();
A: 

Do you mean, you do not want to raise an annoying popup when nothing has been selected? Maybe something like this:

// if your save button is a input type="submit", then maybe
// you would prefer to bind a 'submit' handler to the form instead
$("#save").click(function() {
    var chkbox= $('#divListBox').find(':checked')
                     .map(function() {
                         return $(this).val();
                     }).get();
    if(chkbox.length) {
        $("#myForm").submit(); // or however you are handling this
    } else {

        // populate a container with an error message
        $("#cbError").text("Please select at least one");
    }
    return false;
});
karim79
@ karim79 -- thanks, I'll give it a try.
hersh