Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery validate plugin radio with optional text
    primarykey
    data
    text
    <p>I'm trying to figure out how to validate a form element with a mix of radio inputs and a text input:</p> <pre><code>&lt;label&gt;Question?&lt;/label&gt; &lt;input type="radio" class="mandatory" name="questions[1][]" value="1" /&gt;answer 1&lt;br/&gt; &lt;input type="radio" class="mandatory" name="questions[1][]" value="2" /&gt;answer 2&lt;br/&gt; &lt;input class="ignore" type="radio" id="questions[1][]" /&gt;Other (please specify)&lt;br/&gt; &lt;input class="optional mandatory" type="text" name="questions[1][]" value="" /&gt; </code></pre> <p>I've figured out how to get the form to behave as expected (select and unselect) with the following code:</p> <pre><code>$("input.optional").focus(function () { var this_name = $(this).attr("name"); $("input:radio").filter(function() {return $(this).attr('name') == this_name; }).attr('checked', false); $("input").filter(function() {return $(this).attr('id') == this_name; }).attr('checked', true); }); $(':radio').click(function () { var this_name = $(this).attr("name"); $("input").filter(function() {return $(this).attr('id') == this_name; }).attr('checked', false); $("input.optional").filter(function() {return $(this).attr('name') == this_name; }).val(''); }); </code></pre> <p>I was hoping I could use the class "mandatory" to validate the mix of radio and text inputs:</p> <pre><code>$("form .manditory").each(function () { $(this).rules("add", {required: true}); }); </code></pre> <p>But it's not working as expected. With the radio (id="questions[1][]") selected, and the text input containing content, the form element is still flagged as invalid.</p> <p>Suggestions...maybe a better approach?</p> <p>Thanks in advance.</p> <p><strong>UPDATE</strong></p> <p>Sorry, I should have clarified that I'm using the validate plugin:</p> <pre><code>$("form").validate({ ... }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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