3 Replies Latest reply on Nov 25, 2008 12:26 PM by (Brad_Grasty)

# If box is > 100 or < 0 how do I 0 out that box?

My question is I have a group of 15 boxes titled Factor_weight_1 thru Factor_Weight_14 I also have a 15th box that it titled Factor_Weight_Total. The first 14

Each of the 14 boxes can be between 0 and 100 and when added together must total 100 in the Factor_Weight_Total box.

So I am trying to put on Factor_Weight_1 validation the following code.

event.value = this.getField("Factor_weight_1").value;
if (this.getField("Factor_weight_Total").value > 100) event.value = 0;
if (this.getField("Factor_weight_Total").value < 0) event.value = 0;

Each time I type in any number it will clear the number I put in and replace it with 0.

Any ideas?
• ###### 1. Re: If box is > 100 or < 0 how do I 0 out that box?

If the current field value is less than 0 OR the current field value is greater than 100 then the current field value is equal to 0.

Try this:

if (event.value < 0 || event.value > 100){
event.value = 100;
}
• ###### 2. Re: If box is > 100 or < 0 how do I 0 out that box?
Make sure you have spelled and captilalized your field names exactly the same as the fields. You may also need to set the field calculation order.

Are there any messages in the JavaScirpt Debugging console?

I would have used the custom calculation script, as this field will be recalculcated when any field involved in a calculation is updated, while the 'validation' action only occurs when a field has data entered into the field from the keyboard.

And you can use either:

event.value = this.getField("Factor_Weight_1").value;
if (this.getField("Factor_Weight_Total").value > 100) event.value = 0;
if (this.getField("Factor_Weight_Total").value < 0) event.value = 0;

// or

event.value = this.getField("Factor_Weight_1").value;
if (this.getField("Factor_Weight_Total").value < 0 | this.getField("Factor_Weight_Total").value > 100) event.value = 0;

In the 'Custom Calculation Script'.
• ###### 3. Re: If box is > 100 or < 0 how do I 0 out that box?
Ok I check the capitalization and that is correct. The JavaScript Debugging is very happy with the syntax and throws no errors.

event.value = this.getField("Factor_weight_1").value;
if (this.getField("Factor_weight_total").value < 0 | this.getField("Factor_weight_total").value > 100) event.value = 0;

I have put the script in the validation and in the calculation field both times when I type in anything the field 0s out.

The script Gene Dianoski posted will check the current field but not make sure that the total is < 100 so I edited it

if (this.getField("Factor_weight_total").value < 0 || this.getField("Factor_weight_total").value > 100){
event.value = 0;
}

This works once I got the script in the validate section.

In the calculation field I had to get Factor_weight_1 to sum itself (so it really did not do anything.) This let me set the calculation order to Factor_weight_total and then Factor_weight_1

Now it will 0 itself out if the total is 100 or more.

The problem now is if my Factor_weight_total = 90 and I set Factor_weight_1 that is currently 0 to 15 the Factor_Weight_total = 105and it is happy.

If I then go back to Factor_Weight_1 and set it as 20 because Factor_weight_total = 105 it sets Factor_Weight_1 to 0

Could I use something like a pre addition check validation?
if (this.getField("Factor_weight_total").value + this.getField("Factor_weight_2") < 0 || this.getField("Factor_weight_total").value + this.getField("Factor_weight_2") > 100){
event.value = 0;
}