dinmatt
asked on
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;
}
ASKER
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, here's what I ended up with, I call the 2 functions like such:
onblur="this.value = checkMax(this.value);calcu lateTotals ()"
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.
onblur="this.value = checkMax(this.value);calcu
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.
Your welcome! I'm glad I could help. Thanks for the grade, the points and the fun question.
bol
bol
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