Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, first you need a way to calculate the total of all dynamic fields. One way to do so is by marking all such fields with a class. Let's say we call them "amount". You can then calculate the sum of them with this</p> <pre><code>var total = 0; $('#depositForm input.amount').each(function() { total += Number($(this).val()); }); </code></pre> <p>Now, you'll need to pass this total value to the validation method. Since javascript has closures you can do the following</p> <pre><code>$("#depositForm").validate({ rules: { fullAmount: { depositsSum: function() { var total = 0; $('input.amount').each(function() { total += Number($(this).val()); }); return total; } } } }); </code></pre> <p>This function will be executed every time and its result will be your param value. So the validation function will ultimately be</p> <pre><code>function(value, element, total) { return this.optional(element) || value == total; } </code></pre> <p>For the second part of the question, running the validation for the dynamic fields, there are two approaches. Either have a validation marking class or use the <code>rule("add", )</code> and <code>rule("remove", )</code> methods everything to insert/remove a dynamic field. Here's an example of the class approach: Let all validated fields (dynamic and total) have class "checkSum". Then:</p> <pre><code>$.validator.addClassRules("checkSum", { depositsSum: function() { var total = 0; $('input.amount').each(function() { total += Number($(this).val()); }); return total; } }); $("#depositForm").validate(); </code></pre> <p>Here's a <a href="http://jsfiddle.net/YWRPA/2/" rel="nofollow">JSFiddle of it all</a>.</p> <p>Of course you'll notice that this doesn't work as you might thought it would. For your application the kind of validation you're looking for is full form validation triggered by field change, not field validation. Play around a bit with it and see what I'm talking about</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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