Force at least one field to be required

Asked

Viewed 693 times

2

I have 4 Checkbox in my form HTML, I am working on validations only with Jquery.Validate, there is a way that I define that at least 1 of these checBox is triggered?

Some property of validate? Or just creating a function in race?

I found something on here Apparently the solution seems practical and feasible, but in my case I don’t have a name for the checkedbox group

My code is like this:

<td colspan="2">Você conversou com quem?
   <input type="checkbox" name="Diretor" id="Diretor" />Diretor
   <input type="checkbox" name="Vice" id="Vice" />Vice-Diretor
   <input type="checkbox" name="Coordenador" id="Coordenador" />Coordenador
   <input type="checkbox" name="Nenhum" id="Nenhum"  />Não conversei com ninguem
</td>

1 answer

1


I think what you need is the method require_from_group validates if an item in a group is completed.

On the plugin page (link above) you have this example:

$( "#myform" ).validate({
  rules: {
    mobile_phone: {
      require_from_group: [1, ".phone-group"]
    },
    home_phone: {
      require_from_group: [1, ".phone-group"]
    },
    work_phone: {
      require_from_group: [1, ".phone-group"]
    }
  }
});

In your case the code could be like this: http://jsfiddle.net/n1581gkx/1/

and then you need to have these files loaded:

<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
  • kkkk what I didn’t like was this phone-group . . . it comes from [library]? (http://jqueryvalidation.org/files/dist/additional-methods.min.js)

  • @Marcelobonifazio adapted jsFiddle to use his HTML. But you have to join this library yes.

  • this error: Uncaught Typeerror: Cannot read Property 'param' of null at line 4 of jquery.validate.min.js

  • @Marcelobonifazio can adapt my jsFiddle with your code and put here an example of jsFiddle with the error?

  • old, the problem is that here in the company we use standard libraries, we can not be adapting in this way rs type, the jquery we use is the v1.4.2 yet p.exp.... then we get kind of stuck with our technology :(

  • @Marcelobonifazio then make a custom validate with simple JS... type if( !$('.phone-group').filter(function(){ return this.checked; }) ) return false; // caso nenhum checkbox clicado

  • 1

    I adapted some business here and apparently it worked, at last, grateful old :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.