javascript - jquery.validate: multiple fields add up to value -
this question has answer here:
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
- define validation rule validate total of group of fields against total field
- 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
Post a Comment