alperadatoz@gmail.com said:
[color=blue]
>function calculator(user, number){
> var a=0;
> var b=0;
>
> for(i=1; i<6; i++) {
> if(eval("document.chmod.Matching"+i).checked==true ) {
> a +=(eval("document.chmod.textField"+i+".value")*1);
> }
> else {
> b +=(eval("document.chmod.textField"+i+".value")*1);
> }
> }
> document.chmod.Matching_Funds.value=a
> document.chmod.Other_Funds.value=b
>
>}[/color]
[Most of the problems I'm pointing out appear in the original code,
but it's easier to respond here]
1. Why are user and number passed to this function?
2. You don't need to use eval() in any of these cases.
3. You don't need to compare a boolean value to true:
if(document.chmod.elements["Matching"+i].checked) {
4. The unary + operator is more efficient for converting strings to numbers.
a += +document.chmod.elements["textField"+i].value;
5. The function should be invoked by the onChange handler,
rather than by the onBlur handler.
6. You should provide some error checking for non-numeric entries.
Probably something better than:
if(isNaN(a)) {
document.chmod.Matching_Funds.value="Error - non-numeric entry";
} else {
document.chmod.Matching_Funds.value=a;
}
7. Don't forget to repeat your calculations on the server side.
Never trust dollar values computed on the user's machine.