0

I'm searching a while for this and I can't found something that works for me.

I have this checkbox:

 <%= Html.CheckBox("cbCodigo") %> <label class="inline" for="Codigo">Codigo</label>
 <%= Html.CheckBox("cbNombreCliente") %> <label class="inline" for="NombreCliente">Nombre del cliente</label>
 <%= Html.CheckBox("cbCiudad") %> <label class="inline" for="Ciudad">Ciudad</label>

I want to validate that only one is checked when a textbox change, something like this, and use .validate of jQuery, I don't what is the best way for validate this.

tbCodCliente is a textbox that I use as a search parameter, and the checkbox is a parameter or a value for the autocomplete function of the textbox

$('#tbCodCliente').change(function() {
     if ($('#cbCodigo').attr('checked', false) &&
          $('#cbNombreCliente').attr('checked', false) &&
          $('#tbCheckbox').attr('checked', false)) {

          // function for validate method          
     }
});

I trying to validate of this way, but I don't know if it is the best way.

EDIT: I want something like this, but still can found how make it works

('#tbCodCliente').change(function() {
    if( $("input:checked").length == 0 ) {
        $("#request-form").validate({
             rules: {
                 checkbox: { 
                     required: 'input[type="checkbox"]:checked',
                     minlength: 1
                 }   
             },
             messages: {
                 checkbox: {"Please check at least one."}
             }
        })        

    }
});
0

2 Answers 2

2

If these are the only checkboxes available on the page then you can try following jQuery statement to check:

if( $("input:checked").length == 1 )

Edit:

To be more precise, you can change the validation code to following:

$('#tbCodCliente').change(function() {
   if( $("input:checked").length == 1 ) {
       //Do stuff here
   }
});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks!, yes, there are the only checkbox of the form, but how i use the .validate() of jquery?
And if they're not the only ones, you still are able to refer from the container (via class or id) to it: if( $(".container input:checked").length == 1) { /* ... */ }
yes!! it helps a lot, i'm still searching how to use .validate() if none checkbox are checked
Here is the answer from one of the similar question link
$('input:checkbox[name=name_your_checkbox\[\]]:checked').length == 1
1

This will uncheck other checkboxes once any single checkbox is checked.

$('input:checkbox').change(function () {
        if (this.checked) {
            $('input:checkbox:not(#'+this.id+')').attr('checked', false);
        }
}); 

Demo: http://jsfiddle.net/5btfe/

And for single-checkbox-selected check you can refer epuri's answer which is quite correct.

2 Comments

yes indeed, thanks!. i want to know how i use .validate of jquery, for show an alert if none of the checkbox is checked
you can add the above check into a custom method which will be called directly into validate call.stackoverflow.com/questions/241145/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.