Add to existing function an optional check for minimum value

Posted on 2007-11-19
Medium Priority
Last Modified: 2012-08-13
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

Question by:dinmatt
  • 3
  • 2
LVL 54

Expert Comment

ID: 20314522

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.


Author Comment

ID: 20319261
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!
LVL 54

Accepted Solution

b0lsc0tt earned 2000 total points
ID: 20321196
You would actually be passing the field "object."  You can use ...


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.


Author Comment

ID: 20331741
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.
LVL 54

Expert Comment

ID: 20331801
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question