SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Oct 2, 2007, 09:40 #1
- Join Date
- May 2002
- Posts
- 41
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Adding up the fields to make 100% in a form
Hi
I wondered whether someone can point me to a script which would perform a simple calculation.
In this survey i have created
http://www.altassets.com/surveysmade...2008survey.php
in point 4b
----------------------------
b.) What do you anticipate will be your allocation split between re-ups and new investment in funds in 2008?
Re-ups (textfield name: ReupsSplitPercentage) %
New (textfield name: NewSplitPercentage) %
----------------------------
I need to make sure that the two fields do not add up to more than a 100%. If one number is entered the other gets automaticly adjusted. Alternatively I could add a Total field which would be a sum of the two, and which would display an error message when the sum is bigger than 100%.
At the moment both fields have for a simple validation applied where either can be heigher than 100.
I am a bit of a novice in java script, any help would be very much appreciated...
Many thanks in advance!!
Gayeta
-
Oct 2, 2007, 18:44 #2
- Join Date
- Sep 2007
- Posts
- 136
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
so based on your code, here's an idea:
Code HTML4Strict:<script type="text/javascript"> adjustPercent = function(aVal) { var val = ['ReupsSplitPercentage', 'NewSplitPercentage']; var v1 = document.getElementById(val[aVal]); var v2 = document.getElementById(val[1-aVal]); if(isNaN(v1.value)) return false; v2.value = 100-v1.value; } </script> <td width="235" height="30" align="left" valign="middle"> <b>Re-ups</b> <label> <input name="ReupsSplitPercentage" type="text" class="text" id="ReupsSplitPercentage" onBlur="adjustPercent(0)" size="5"> %</label></td> <td width="365" align="left" valign="middle"> <b>New</b> <label><input name="NewSplitPercentage" type="text" class="text" id="NewSplitPercentage" onBlur="adjustPercent(1)" size="5"> %</label> <br> </td>
that doesnt check for percentages higher than 100, just resets the other percentage, should get you started
Bookmarks