SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    0 Post(s)
    0 Thread(s)

    Unhappy Adding up the fields to make 100% in a form


    I wondered whether someone can point me to a script which would perform a simple calculation.

    In this survey i have created

    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!!


  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    0 Post(s)
    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]);
    		return false;
    	v2.value = 100-v1.value;
    	<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">
    	<td width="365" align="left" valign="middle">
    	<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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts