javascript - jquery.validate: multiple fields add up to value -


i'm working on form user enters total, enters more values other fields represent dividing of total. example:

<input type="text" name="total" /> <input type="text" name="portion[1]" /> <input type="text" name="portion[2]" /> <!-- , on --> <input type="text" name="portion[n]" /> 

if user enters 123.45 total, need fill out portions 1 - n such values add 123.45. each portion field required positive number or 0 other restrictions on them.

the jquery.validate plugin has equalto validation method, can seem cope single field, rather set.

is there way to

  1. define validation rule validate total of group of fields against total field
  2. get single message display group of fields if don't add up

try function jquery event

  function mvalidate()     {     var total=$('[name=total]').val();      var n=10; // no of portions      var partialsum=0;     for(var i=0;i<n; i++)     {     var t=$("[name=portion["+i+"]]").val();     partialsum+=parsefloat(t);      }      if(partialsum<total)     alert("portions add not complete!");      }  $("#checkbutton").click(function()   {   mvalidate();   }); 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -