Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Calculating form field values

Calculating form field values

Scheduled Pinned Locked Moved ASP.NET
csharpdatabaseregexhelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AdamskiR
    wrote on last edited by
    #1

    Hi I have a form set up currently with a number of fields which the user must submit numeric data to. Which then gets stored into a database which I now have all working fine.:) 2 of the fields will include totals of data which they will enter: The total amount of people The total amount problems with people (a person could have more than one problem) Then there are 3 different categories of questions which someone must fill in. For one category it has 3 fields to fill in, what i would like is that the 3 values added must match the total amount added of problems with people. Another category has 5 fields to fill in which should match the total number of people filled in earlier in the form. If someone tries to submit the form with an incorrect match of people to the totals it wont allow them to sumbit the form. Could anyone give me some pointers as to how best to do this? I use C# Thanks

    M 1 Reply Last reply
    0
    • A AdamskiR

      Hi I have a form set up currently with a number of fields which the user must submit numeric data to. Which then gets stored into a database which I now have all working fine.:) 2 of the fields will include totals of data which they will enter: The total amount of people The total amount problems with people (a person could have more than one problem) Then there are 3 different categories of questions which someone must fill in. For one category it has 3 fields to fill in, what i would like is that the 3 values added must match the total amount added of problems with people. Another category has 5 fields to fill in which should match the total number of people filled in earlier in the form. If someone tries to submit the form with an incorrect match of people to the totals it wont allow them to sumbit the form. Could anyone give me some pointers as to how best to do this? I use C# Thanks

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi there. Just a suggestion - do you need them to enter the totals at all? How about if you just have them enter the 3 fields (or 5 fields), then you compute the total on the server side? If you want to show the total as they are entering values, you could add a little client-side javascript that adds up the fields as they enter values dynamically (perhaps using onkeydown or onchange events client-side).

      A 1 Reply Last reply
      0
      • M Mike Ellison

        Hi there. Just a suggestion - do you need them to enter the totals at all? How about if you just have them enter the 3 fields (or 5 fields), then you compute the total on the server side? If you want to show the total as they are entering values, you could add a little client-side javascript that adds up the fields as they enter values dynamically (perhaps using onkeydown or onchange events client-side).

        A Offline
        A Offline
        AdamskiR
        wrote on last edited by
        #3

        Hi Thanks for the reply The totals do need to be added as they want to be able to view totals quickly from a results page we have created which totals all figures from certain categories. Could you point me in the direction of any javascripts that I can download to do this?;) Thanks

        M 1 Reply Last reply
        0
        • A AdamskiR

          Hi Thanks for the reply The totals do need to be added as they want to be able to view totals quickly from a results page we have created which totals all figures from certain categories. Could you point me in the direction of any javascripts that I can download to do this?;) Thanks

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Here's a simple example. This is a straight .htm page (doesn't need to be an .aspx) showing the simple javascript applied to each text box.

          <html>
          <head>
          <script type="text/javascript">
          function ComputeTotal()
          {
          var t1 = document.getElementById("text1");
          var t2 = document.getElementById("text2");
          var t3 = document.getElementById("text3");

              // if any of the boxes contain characters that are not numeric,
              // treat it as a zero
              var v1 = (isNaN(parseInt(t1.value)) ? 0 : parseInt(t1.value));
              var v2 = (isNaN(parseInt(t2.value)) ? 0 : parseInt(t2.value));
              var v3 = (isNaN(parseInt(t3.value)) ? 0 : parseInt(t3.value));
              
              return (v1 + v2 + v3);
            }
            
            function OutputTotal()
            {
              var vTotal = ComputeTotal();                
              var tTotal = document.getElementById("spanTotal");
              tTotal.innerHTML = vTotal;
            }
          </script>
          

          </head>

          <body>
          <h3>Simple JavaScript to compute a total</h3>
          This one uses the <code>onkeyup</code> event of the text boxes to compute the totals.
          <br />
          Enter three values:
          <br/>
          <input type="text" id="text1" onkeyup="OutputTotal();"/><br />
          <input type="text" id="text2" onkeyup="OutputTotal();"/><br />
          <input type="text" id="text3" onkeyup="OutputTotal();"/><br />
          Total: <span id="spanTotal" style="font-weight: bold;"></span>
          <br />
          </body>

          </html>

          A 1 Reply Last reply
          0
          • M Mike Ellison

            Here's a simple example. This is a straight .htm page (doesn't need to be an .aspx) showing the simple javascript applied to each text box.

            <html>
            <head>
            <script type="text/javascript">
            function ComputeTotal()
            {
            var t1 = document.getElementById("text1");
            var t2 = document.getElementById("text2");
            var t3 = document.getElementById("text3");

                // if any of the boxes contain characters that are not numeric,
                // treat it as a zero
                var v1 = (isNaN(parseInt(t1.value)) ? 0 : parseInt(t1.value));
                var v2 = (isNaN(parseInt(t2.value)) ? 0 : parseInt(t2.value));
                var v3 = (isNaN(parseInt(t3.value)) ? 0 : parseInt(t3.value));
                
                return (v1 + v2 + v3);
              }
              
              function OutputTotal()
              {
                var vTotal = ComputeTotal();                
                var tTotal = document.getElementById("spanTotal");
                tTotal.innerHTML = vTotal;
              }
            </script>
            

            </head>

            <body>
            <h3>Simple JavaScript to compute a total</h3>
            This one uses the <code>onkeyup</code> event of the text boxes to compute the totals.
            <br />
            Enter three values:
            <br/>
            <input type="text" id="text1" onkeyup="OutputTotal();"/><br />
            <input type="text" id="text2" onkeyup="OutputTotal();"/><br />
            <input type="text" id="text3" onkeyup="OutputTotal();"/><br />
            Total: <span id="spanTotal" style="font-weight: bold;"></span>
            <br />
            </body>

            </html>

            A Offline
            A Offline
            AdamskiR
            wrote on last edited by
            #5

            Thanks for this, that works great, however:rolleyes: what im looking to do is put the results in a hidden text field which I then put a compare validator on in visual studio. Ill have a go at fiddling with this to do it, but any tips will be appreciated!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups