Add input fields and validate the sum against total using javascript









up vote
0
down vote

favorite












I am trying to get the sum of the values entered into the age input fields upon change. Then I need to validate the sum of ages against the total number of students trained. If the total number of students trained does not match the sum of the students ages I want to throw an error. I am coding in C#.



Here is my html code:



<h4>Training</h4>
<b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
<h5>Ages of Students </h5>
<table>
<tr>
<td><label for="age 18" class="float-left"> <18:</label> <input type="number" min="0" max="500" name="age 18" oninput="calcage"> </td>
<td><label for="age 24" class="float-left">18-24:</label> <input type="number" min="0" name="age 24 oninput="calcage"> </td>
<td><label for="age 34" class="float-left">25-34:</label> <input type="number"min="0" name="age 34" oninput="calcage"> </td>
<td><label for="age 44" class="float-left">35-44:</label> <input type="number" min="0" name="age 44" oninput="calcage"> </td>
</tr>
</table>


Here is my javascript:



function calcage() 
var score = 0;
$(".age:input").each(function ()
score += parseInt(this.value);
);
$("input[name=Score]").val(score)


$().ready(function ()
$(".age").change(function ()
calcage()
);
);









share|improve this question



















  • 2




    You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
    – zfrisch
    Nov 9 at 18:58











  • Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
    – Rufus L
    Nov 9 at 18:58










  • The inputs don't have a class="age" which is what your selector is looking for
    – charlietfl
    Nov 9 at 19:08














up vote
0
down vote

favorite












I am trying to get the sum of the values entered into the age input fields upon change. Then I need to validate the sum of ages against the total number of students trained. If the total number of students trained does not match the sum of the students ages I want to throw an error. I am coding in C#.



Here is my html code:



<h4>Training</h4>
<b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
<h5>Ages of Students </h5>
<table>
<tr>
<td><label for="age 18" class="float-left"> <18:</label> <input type="number" min="0" max="500" name="age 18" oninput="calcage"> </td>
<td><label for="age 24" class="float-left">18-24:</label> <input type="number" min="0" name="age 24 oninput="calcage"> </td>
<td><label for="age 34" class="float-left">25-34:</label> <input type="number"min="0" name="age 34" oninput="calcage"> </td>
<td><label for="age 44" class="float-left">35-44:</label> <input type="number" min="0" name="age 44" oninput="calcage"> </td>
</tr>
</table>


Here is my javascript:



function calcage() 
var score = 0;
$(".age:input").each(function ()
score += parseInt(this.value);
);
$("input[name=Score]").val(score)


$().ready(function ()
$(".age").change(function ()
calcage()
);
);









share|improve this question



















  • 2




    You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
    – zfrisch
    Nov 9 at 18:58











  • Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
    – Rufus L
    Nov 9 at 18:58










  • The inputs don't have a class="age" which is what your selector is looking for
    – charlietfl
    Nov 9 at 19:08












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to get the sum of the values entered into the age input fields upon change. Then I need to validate the sum of ages against the total number of students trained. If the total number of students trained does not match the sum of the students ages I want to throw an error. I am coding in C#.



Here is my html code:



<h4>Training</h4>
<b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
<h5>Ages of Students </h5>
<table>
<tr>
<td><label for="age 18" class="float-left"> <18:</label> <input type="number" min="0" max="500" name="age 18" oninput="calcage"> </td>
<td><label for="age 24" class="float-left">18-24:</label> <input type="number" min="0" name="age 24 oninput="calcage"> </td>
<td><label for="age 34" class="float-left">25-34:</label> <input type="number"min="0" name="age 34" oninput="calcage"> </td>
<td><label for="age 44" class="float-left">35-44:</label> <input type="number" min="0" name="age 44" oninput="calcage"> </td>
</tr>
</table>


Here is my javascript:



function calcage() 
var score = 0;
$(".age:input").each(function ()
score += parseInt(this.value);
);
$("input[name=Score]").val(score)


$().ready(function ()
$(".age").change(function ()
calcage()
);
);









share|improve this question















I am trying to get the sum of the values entered into the age input fields upon change. Then I need to validate the sum of ages against the total number of students trained. If the total number of students trained does not match the sum of the students ages I want to throw an error. I am coding in C#.



Here is my html code:



<h4>Training</h4>
<b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
<h5>Ages of Students </h5>
<table>
<tr>
<td><label for="age 18" class="float-left"> <18:</label> <input type="number" min="0" max="500" name="age 18" oninput="calcage"> </td>
<td><label for="age 24" class="float-left">18-24:</label> <input type="number" min="0" name="age 24 oninput="calcage"> </td>
<td><label for="age 34" class="float-left">25-34:</label> <input type="number"min="0" name="age 34" oninput="calcage"> </td>
<td><label for="age 44" class="float-left">35-44:</label> <input type="number" min="0" name="age 44" oninput="calcage"> </td>
</tr>
</table>


Here is my javascript:



function calcage() 
var score = 0;
$(".age:input").each(function ()
score += parseInt(this.value);
);
$("input[name=Score]").val(score)


$().ready(function ()
$(".age").change(function ()
calcage()
);
);






javascript c# webmatrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:07

























asked Nov 9 at 18:55









tnbumbray

931113




931113







  • 2




    You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
    – zfrisch
    Nov 9 at 18:58











  • Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
    – Rufus L
    Nov 9 at 18:58










  • The inputs don't have a class="age" which is what your selector is looking for
    – charlietfl
    Nov 9 at 19:08












  • 2




    You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
    – zfrisch
    Nov 9 at 18:58











  • Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
    – Rufus L
    Nov 9 at 18:58










  • The inputs don't have a class="age" which is what your selector is looking for
    – charlietfl
    Nov 9 at 19:08







2




2




You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
– zfrisch
Nov 9 at 18:58





You have to show us what you've attempted as far as functionality. StackOverflow isn't a coding service. You didn't so much as specify a language.
– zfrisch
Nov 9 at 18:58













Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
– Rufus L
Nov 9 at 18:58




Yeah, it's not clear from what you've shown where you are stuck. Can you show the code? Also, is this javascript or c#
– Rufus L
Nov 9 at 18:58












The inputs don't have a class="age" which is what your selector is looking for
– charlietfl
Nov 9 at 19:08




The inputs don't have a class="age" which is what your selector is looking for
– charlietfl
Nov 9 at 19:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your HTML has a few errors:



  1. Your <input> elements need to have class="age" added to them so that they are recognized by the $(".age:input").each() function.

  2. The name of your second <input> element is missing the closing double quote (").

  3. You are missing the parentheses (()) after the calcage calls in your oninput events. However, since you are using the $(".age").change() function to call the calcage() function, it is not necessary to also have the oninput event call the calcage() function. Also, the oninput event is called every time an input changes without waiting until all changes are complete. This means that if a student entered an age of "30", the calcage function would be called twice: first for the "3", and then again for the "0". Since this is not what you want, those event handlers can be removed from the inputs. Leave $(".age").change() since this will only fire once the student has finished typing. It will work once your <input> elements have the class="age" added to them.

  4. The <label> elements for attribute will only work when there is an <input> element with a matching id value.

Try the following corrected HTML:



<h4>Training</h4>
<b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
<h5>Ages of Students </h5>
<table>
<tr>
<td><label for="age 18" class="float-left"> <18:</label> <input class="age" type="number" min="0" max="500" name="age 18"> </td>
<td><label for="age 24" class="float-left">18-24:</label> <input class="age" type="number" min="0" name="age 24"> </td>
<td><label for="age 34" class="float-left">25-34:</label> <input class="age" type="number" min="0" name="age 34"> </td>
<td><label for="age 44" class="float-left">35-44:</label> <input class="age" type="number" min="0" name="age 44"> </td>
</tr>
</table>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231771%2fadd-input-fields-and-validate-the-sum-against-total-using-javascript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Your HTML has a few errors:



    1. Your <input> elements need to have class="age" added to them so that they are recognized by the $(".age:input").each() function.

    2. The name of your second <input> element is missing the closing double quote (").

    3. You are missing the parentheses (()) after the calcage calls in your oninput events. However, since you are using the $(".age").change() function to call the calcage() function, it is not necessary to also have the oninput event call the calcage() function. Also, the oninput event is called every time an input changes without waiting until all changes are complete. This means that if a student entered an age of "30", the calcage function would be called twice: first for the "3", and then again for the "0". Since this is not what you want, those event handlers can be removed from the inputs. Leave $(".age").change() since this will only fire once the student has finished typing. It will work once your <input> elements have the class="age" added to them.

    4. The <label> elements for attribute will only work when there is an <input> element with a matching id value.

    Try the following corrected HTML:



    <h4>Training</h4>
    <b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
    <h5>Ages of Students </h5>
    <table>
    <tr>
    <td><label for="age 18" class="float-left"> <18:</label> <input class="age" type="number" min="0" max="500" name="age 18"> </td>
    <td><label for="age 24" class="float-left">18-24:</label> <input class="age" type="number" min="0" name="age 24"> </td>
    <td><label for="age 34" class="float-left">25-34:</label> <input class="age" type="number" min="0" name="age 34"> </td>
    <td><label for="age 44" class="float-left">35-44:</label> <input class="age" type="number" min="0" name="age 44"> </td>
    </tr>
    </table>





    share|improve this answer


























      up vote
      0
      down vote













      Your HTML has a few errors:



      1. Your <input> elements need to have class="age" added to them so that they are recognized by the $(".age:input").each() function.

      2. The name of your second <input> element is missing the closing double quote (").

      3. You are missing the parentheses (()) after the calcage calls in your oninput events. However, since you are using the $(".age").change() function to call the calcage() function, it is not necessary to also have the oninput event call the calcage() function. Also, the oninput event is called every time an input changes without waiting until all changes are complete. This means that if a student entered an age of "30", the calcage function would be called twice: first for the "3", and then again for the "0". Since this is not what you want, those event handlers can be removed from the inputs. Leave $(".age").change() since this will only fire once the student has finished typing. It will work once your <input> elements have the class="age" added to them.

      4. The <label> elements for attribute will only work when there is an <input> element with a matching id value.

      Try the following corrected HTML:



      <h4>Training</h4>
      <b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
      <h5>Ages of Students </h5>
      <table>
      <tr>
      <td><label for="age 18" class="float-left"> <18:</label> <input class="age" type="number" min="0" max="500" name="age 18"> </td>
      <td><label for="age 24" class="float-left">18-24:</label> <input class="age" type="number" min="0" name="age 24"> </td>
      <td><label for="age 34" class="float-left">25-34:</label> <input class="age" type="number" min="0" name="age 34"> </td>
      <td><label for="age 44" class="float-left">35-44:</label> <input class="age" type="number" min="0" name="age 44"> </td>
      </tr>
      </table>





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Your HTML has a few errors:



        1. Your <input> elements need to have class="age" added to them so that they are recognized by the $(".age:input").each() function.

        2. The name of your second <input> element is missing the closing double quote (").

        3. You are missing the parentheses (()) after the calcage calls in your oninput events. However, since you are using the $(".age").change() function to call the calcage() function, it is not necessary to also have the oninput event call the calcage() function. Also, the oninput event is called every time an input changes without waiting until all changes are complete. This means that if a student entered an age of "30", the calcage function would be called twice: first for the "3", and then again for the "0". Since this is not what you want, those event handlers can be removed from the inputs. Leave $(".age").change() since this will only fire once the student has finished typing. It will work once your <input> elements have the class="age" added to them.

        4. The <label> elements for attribute will only work when there is an <input> element with a matching id value.

        Try the following corrected HTML:



        <h4>Training</h4>
        <b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
        <h5>Ages of Students </h5>
        <table>
        <tr>
        <td><label for="age 18" class="float-left"> <18:</label> <input class="age" type="number" min="0" max="500" name="age 18"> </td>
        <td><label for="age 24" class="float-left">18-24:</label> <input class="age" type="number" min="0" name="age 24"> </td>
        <td><label for="age 34" class="float-left">25-34:</label> <input class="age" type="number" min="0" name="age 34"> </td>
        <td><label for="age 44" class="float-left">35-44:</label> <input class="age" type="number" min="0" name="age 44"> </td>
        </tr>
        </table>





        share|improve this answer














        Your HTML has a few errors:



        1. Your <input> elements need to have class="age" added to them so that they are recognized by the $(".age:input").each() function.

        2. The name of your second <input> element is missing the closing double quote (").

        3. You are missing the parentheses (()) after the calcage calls in your oninput events. However, since you are using the $(".age").change() function to call the calcage() function, it is not necessary to also have the oninput event call the calcage() function. Also, the oninput event is called every time an input changes without waiting until all changes are complete. This means that if a student entered an age of "30", the calcage function would be called twice: first for the "3", and then again for the "0". Since this is not what you want, those event handlers can be removed from the inputs. Leave $(".age").change() since this will only fire once the student has finished typing. It will work once your <input> elements have the class="age" added to them.

        4. The <label> elements for attribute will only work when there is an <input> element with a matching id value.

        Try the following corrected HTML:



        <h4>Training</h4>
        <b>Total number of students in training:</b> <input type="number" name="Total Number students trained" min="0"><span style="color: #f00">
        <h5>Ages of Students </h5>
        <table>
        <tr>
        <td><label for="age 18" class="float-left"> <18:</label> <input class="age" type="number" min="0" max="500" name="age 18"> </td>
        <td><label for="age 24" class="float-left">18-24:</label> <input class="age" type="number" min="0" name="age 24"> </td>
        <td><label for="age 34" class="float-left">25-34:</label> <input class="age" type="number" min="0" name="age 34"> </td>
        <td><label for="age 44" class="float-left">35-44:</label> <input class="age" type="number" min="0" name="age 44"> </td>
        </tr>
        </table>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 19:32

























        answered Nov 9 at 19:16









        Justin T.

        544213




        544213



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231771%2fadd-input-fields-and-validate-the-sum-against-total-using-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus