Add to existing function an optional check for minimum value

I have form on a .ASP form.  Several fields in the form have an onChange event that calls a javascript function to calculate totals. This works great. However, for only some of the fields, I need to validate that the user does not enter a number greater than the maximum value allowed prior to calculating the totals. If the field is over the max, I would like to display a message and then change the value to either 0 or the max allowed prior to calculating the total. I am not sure how to handle this since not all fields need to be validated but all fields DO have to call the CalculateTotals function. Can someone provide code to accomplish this? I include my function here. Thank you!
<script language="javaScript">
      function calculateTotals() {
 
            MyForm.row9col1.value = Number(MyForm.row1col1.value) + Number(MyForm.row2col1.value)+ Number(MyForm.row3col1.value)+ Number(MyForm.row4col1.value)+ Number(MyForm.row5col1.value)+ Number(MyForm.row6col1.value)+ Number(MyForm.row7col1.value)+ Number(MyForm.row8col1.value);
            MyForm.row9col2.value = Number(MyForm.row1col2.value) + Number(MyForm.row2col2.value)+ Number(MyForm.row3col2.value)+ Number(MyForm.row4col2.value)+ Number(MyForm.row5col2.value)+ Number(MyForm.row6col2.value)+ Number(MyForm.row7col2.value)+ Number(MyForm.row8col2.value);
            MyForm.row9col3.value = Number(MyForm.row1col3.value) + Number(MyForm.row2col3.value)+ Number(MyForm.row3col3.value)+ Number(MyForm.row4col3.value)+ Number(MyForm.row5col3.value)+ Number(MyForm.row6col3.value)+ Number(MyForm.row7col3.value)+ Number(MyForm.row8col3.value);
            MyForm.row9col4.value = Number(MyForm.row1col4.value) + Number(MyForm.row2col4.value)+ Number(MyForm.row3col4.value)+ Number(MyForm.row4col4.value)+ Number(MyForm.row5col4.value)+ Number(MyForm.row6col4.value)+ Number(MyForm.row7col4.value)+ Number(MyForm.row8col4.value);
            MyForm.row9col5.value = Number(MyForm.row1col5.value) + Number(MyForm.row2col5.value)+ Number(MyForm.row3col5.value)+ Number(MyForm.row4col5.value)+ Number(MyForm.row5col5.value)+ Number(MyForm.row6col5.value)+ Number(MyForm.row7col5.value)+ Number(MyForm.row8col5.value);
            
            MyForm.TotalEntry.value = (Number(MyForm.row9col1.value) + Number(MyForm.row9col2.value)+Number(MyForm.row9col3.value) + Number(MyForm.row9col4.value)+ Number(MyForm.row9col5.value))*15;            
      }

Open in new window

dinmattAsked:
Who is Participating?
 
b0lsc0ttIT ManagerCommented:
You would actually be passing the field "object."  You can use ...

onchange="checkMax(this);"

Either the onchange or the onblur event would work, depending on the type of element.

In the function you would use this with something like ...

function checkMax(ele) {
    //  ele is the field object
    //  ele.value is the value entered in that field
    //    other properties/methods can be accessed too

Let me know if you have any other questions or need more info.

bol
0
 
b0lsc0ttIT ManagerCommented:
dinmatt,

From what you have described I would do this by adding another function to the onchange event.  Add it before the calculateTotals() function.  For example if you called it checkMax() then the onchange event would look like ...

onchange="checkMax(); calculateTotals();"

In the checkMax function you should have it retun false if there is a problem.  This should prevent the calculateTotals function from running.  Let me know if you need a suggestion or code for this checkMax function.  If the maximum may be different from field to field then you could even pass the figure as an argument to the function.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
dinmattAuthor Commented:
Ok, that's great. I am new to scripting in Javascript so could you provide a sample code to accomplish this? I am not sure how to pass the current field name to the function to have it check its value so that I can use the function with multiple fields. thanks!
0
 
dinmattAuthor Commented:
Thanks, here's what I ended up with, I call the 2 functions  like such:
onblur="this.value = checkMax(this.value);calculateTotals()"

I found the onblur worked best

Then CheckMax function looks like is:

            function checkMax(num) {
                  if (num == '') return num;
                        if (parseInt(num) > 1 ) {
                          alert('Only 1 team is allowed for this relay!');
                          return 1;
                  } else return num;
            }
That way, it sets it to the default value of 1 if its > 1 and calculates the totals correctly.
0
 
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.