Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is overly complex and I'm not sure <code>require_from_group</code> is the appropriate method for what you're trying to do. It's only supposed to make any specific number of elements <em>FROM</em> a group of elements required. In other words, if you have six items in your group and you specify "2" as the argument, then this method/rule will only be satisfied if <strong><em>any</em></strong> two <em>fields</em> from your group of six are filled out.</p> <p>In your case, you want everything within one group filled out as opposed to everything in another group filled out, which is something else entirely.</p> <p>With jQuery Validate, you cannot switch validation on/off dynamically. Once defined and initialized, it stays "on". However, you can use <a href="http://jqueryvalidation.org/rules/" rel="nofollow">the <code>.rules()</code> method</a> to dynamically over-write, add or remove any rule on any field(s) at any time.</p> <p>I would make the user physically select one group or the other by clicking a radio or checkbox. This will also reduce confusion about what the user is expected to fill in. However, if you don't like that idea, you could instead use the <code>blur</code>, <code>change</code>, or another event on any field to trigger similar "rule altering" code.</p> <p>Then based on the event, you can modify your rules accordingly.</p> <pre><code>$('input[name="radio"]:radio').on('change', function () { // on selection event if ($('#one').is(':checked')) { // if first radio is selected $('.inputGroup1 ').each(function () { // add rules to first group $(this).rules('add', { required: true }); }); $('.inputGroup2').each(function () { // remove rules from second group $(this).rules('remove'); }); } else { // if second radio is selected $('.inputGroup2').each(function () { // add rules to second group $(this).rules('add', { required: true }); }); $('.inputGroup1').each(function () { // remove rules from first group $(this).rules('remove'); }); }; }); </code></pre> <p><strong>Proof of Concept: <a href="http://jsfiddle.net/tF8Wp/" rel="nofollow">http://jsfiddle.net/tF8Wp/</a></strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload