Solved

Add to existing function an optional check for minimum value

Posted on 2007-11-19
5
520 Views
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

0
Comment
Question by:dinmatt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20314522
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
 

Author Comment

by:dinmatt
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!
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 20321196
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
 

Author Comment

by:dinmatt
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.
0
 
LVL 54

Expert Comment

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

bol
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

688 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