Solved

Add to existing function an optional check for minimum value

Posted on 2007-11-19
5
515 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
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now