Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery validation add inputs required dynamically
    primarykey
    data
    text
    <p>I am currently using the <a href="http://jqueryvalidation.org/">bassistance jquery validation</a> or the jqueryvalidation plugin(they renamed it) and I have it validating a form like so:</p> <pre><code>if($.fn.validate) { $("#frmNewOrder").validate({ rules: { txtAmount: { required: true, digits: true }, ddlAccount: { required: true }, ddlBank: { required: true } } }); } </code></pre> <p>Now depending on the amount I need to require or not an extra field a dropdownlist and depending on that dropdownlist another text input. so it's like:</p> <pre><code> if($('#txtAmount').val() &gt;= 10000){ //add ddlReason required } if($('#ddlReason').val() == 'o'){ //add txtReason required } </code></pre> <p>I've tried adding the css class 'required' like the other fields but I take it if they are not inside the rules then it doesn't work? I've tried adding rules like this:</p> <pre><code> $( "#ddlReason" ).rules( "add", { required: true}); </code></pre> <p>and that doesn't work either. any suggestions?</p> <p>EDIT: Here's the <a href="http://asantillan.com/jquery.validate.js">link</a> to the jquery validate that I'm using. if I used rules outside it gives this error: </p> <pre><code>Uncaught TypeError: Cannot read property 'settings' of undefined </code></pre> <p>on line 124:</p> <pre><code>var settings = $.data(element.form, 'validator').settings; </code></pre> <p>and using the suggestions I've seen in other places i've done this which is what causes the error:</p> <pre><code>var defaultrules = { txtAmount: { required: true, digits: true } } if($.fn.validate) { $("#frmNewOrder").validate(); addRules(defaultrules); } </code></pre> <p>EDIT 2: here's the html markup, hope it helps.</p> <pre><code>&lt;form id="frmNewOrder" name="frmNewOrder" action="" method="post"&gt; &lt;label&gt;Amount&lt;/label&gt; &lt;input type="text" name="txtAmount" id="txtAmount" class="required" /&gt; &lt;button id="calculate" type="button"&gt;Calculate&lt;/button&gt; &lt;div id="dvreason" style="display:none;"&gt; &lt;select id="ddlReason" name="ddlReason"&gt; &lt;option&gt;&lt;/option&gt; &lt;option value="i"&gt;Option 1&lt;/option&gt; &lt;option value="o"&gt;Other&lt;/option&gt; &lt;/select&gt; &lt;div id="dvother" style="display:none"&gt; &lt;label&gt;Other reason&lt;/label&gt; &lt;input type="text" name="txtOther" id="txtOther" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;input type="submit" name="send" id="send" value="Send" /&gt; &lt;/form&gt; </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.
 

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