Note that there are some explanatory texts on larger screens.

plurals
  1. PORequired form field only if previous field is filled
    primarykey
    data
    text
    <p><strong>Intro</strong></p> <p>I'm developing a wordpress website where I have a form. The validation of that form is made using jQuery Inline Form Validation Engine 2.2.</p> <p>I have some radio buttons that have two options Yes/No, followed by a required field that only appears if someone chose "No", to explain why. Something like the following example:</p> <blockquote> <p>1 - Is "John Matrix" cooler than "John Rambo"?</p> <p>&rarr; Yes</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; No</p> <p>&nbsp;</p> <p>2 - Is "Commando" the best movie ever?</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;Yes</p> <p>&rarr; No</p> <p>Why not? <strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>_</strong> (* required)</p> </blockquote> <p>So, if someone would say "No", the "Why not" field appears:</p> <pre><code>if( $('#no_commando_field').is(':checked')) { $('#why_not_field').show(); } else { $('#why_not_field').hide(); } </code></pre> <p>&nbsp;</p> <p><strong>Problem</strong></p> <p>My problem is that the "Why not" field is required, even if someone selects the "Yes" option (like it's supposed to). I would like to make this field required only if the previous answer is "No".</p> <p><strong>Quest to solve problem</strong></p> <p>While exploring the code (I'm not a back-end specialist) I realized that the Inline Form Validation Engine adds a class to the required fields, called "validate[required]". I tried deleting that class using jQuery, but the validation still occurs.</p> <pre><code>var validate; validate = $('#why_not_field').attr('class'); validate = validate.replace('validate[required]','not_validated'); $('#why_not_field').attr('class',validate); if ( $('#no_commando_field').is(':checked')) { validate = validate.replace('not_validated','validate[required]'); $('#why_not_field').attr('class',validate); } else { validate = validate.replace('validate[required]','not_validated'); $('#why_not_field').attr('class',validate); } </code></pre> <p>The class swap is working perfectly, but somehow the validation is still occurring. Although this solution worked for me on previous forms (only on the wordpress backoffice), I'm not sure it is the right path to solve this issue.</p> <p>I would like someone to point me in the right direction so I can understand this problem better, in order to solve it. Thanks in advance.</p>
    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.
 

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