Expand my Community achievements bar.

Percent validation

Avatar

Former Community Member
I have a set of numbers that the total should not exceed 100. Otherwise throws an error to the user and SetFocus on the last entered number until the user corrects the number which the total goes under 100. I have the validation FormCal code for the total cell in the validation section in script editor, but dont work at all. What do I need to do?



if (Row4[17].CPCTotalPercent >"100") then



xfa.host.messageBox("Total more than 100%. Please change the percent.")

xfa.host.setFocus($.somExpression)

endif
12 Replies

Avatar

Former Community Member
I'm assuming that each row has a set of numbers and that each row has a field named "CPCTotalPercent" which is the summation of all the numbers for a particular row. Your validation script is then attempting to verify that the sum of all the numbers for that row doesn't exceed 100%. Is that correct?



If so, how is the value of the CPCTotalPercent field being calculated? Are you using a Calculation script?



Since we're talking about percentages and totals, are all the number fields as well as the CPCTotalPercent fields numeric or decimal fields?



I should probably also ask what you mean by "it doesn't work at all": Do you mean the IF statement is never true even if you know the CPCTotalPercent field's value is over 100 or do you mean that the focus is never set on the current field?



At first sight, it would seem that you should try comparing the value of the CPCTotalPercent field to the
number 100 instead of the string "100" as follows:



if (Row4[17].CPCTotalPercent > 100) then...


but without knowing a little more about your form, I can't be certain that this would fix the problem.



There may also be a problem with the time at which you're attempting to set focus back to the field that contains the value which is pushing the total percentage above 100%. It's possible that the setFocus call isn't taking effect because it occurs before Acrobat decides to automatically move focus to the next field in tab order. Are you calling the xfa.host.setFocus function from a number field's Validate event?



Stefan

Adobe Systems

Avatar

Former Community Member
Thanks for the reply,

The script is under Exit section of each numeric field and I have a script that calculates the sum of all numbers on CPCTotalPercent. After entering a number in the numeric filed if the total sum is more than 100 I get the defined error message but I dont get the setFouce back to the field, it keeps going on other fields!

On the other hand I have the same problem for a Date Filed with MM/DD/YYYY pattern. I dont know how to write the pattern in the IF clause and set the setFoucus back to the field when an invalid pattern enters.



Thanks again

Avatar

Former Community Member
OK, so the IF statement is good but you can't get the focus to go back to the field that was just edited. I think the problem, then, is the use of the
somExpression property as the value being passed to the xfa.host.setFocus function.



The xfa.host.setFocus function expects a
unique object name. Assuming the "$" (this) object is uniquely-named, try this:



xfa.host.setFocus($.name)


That should fix the setFocus problem.



Now you also mentioned that you don't know how to write the "MM/DD/YYYY" pattern in the IF statement. Are you trying to validate the date value that was entered against the "MM/DD/YYYY" format?



Stefan

Adobe Systems

Avatar

Former Community Member
Dear Stefan

I really appreciate your helpful answer.



Yes, I have a date field and the pattern is : MM/DD/YYYY. What I want is if the user enters any other format of date an error message appears and cursor gets back to the same field until the user enters a proper pattern.



Thanks

Maryam

Avatar

Former Community Member
In that case, you would use the FormCalc
Date2Num function which lets you specify a date in string format and then the format it should be adhering to:



Date2Num($.rawValue, "MM/DD/YYYY")


If this function returns 0, $.rawValue contains a date in a format which does not match "MM/DD/YYYY".



I've attached a sample form which demonstrates the use of this function.



Stefan

Adobe Systems

Avatar

Former Community Member
Thanks Stefan,

I checked your sample. It works perfect. But the weird thing is that when I choose from the calendar, I get the error message and cant go on until I enter the same date manually!

Why is that?



Maryam

Avatar

Former Community Member
Good point. The problem here is that picking a date from the drop-down calendar will set the field to a date value with the YYYY-MM-DD format which makes things a little more complicated to verify.



If you keep the code as I had demonstrated it, the following line would always fail with a date in the YYYY-MM-DD format:



if (Date2Num($.rawValue, "MM/DD/YYYY") <= 0)


This is because $.rawValue is "2006-06-28", for example, as opposed to "06/28/2006".



The trick here is to use the field's
formatted value (its raw value formatted with its Display Pattern) in the Date2Num function as opposed to the field's
raw value. By setting the Display Pattern to "MM/DD/YYYY" and then using the following code,



if (Date2Num($.formattedValue, "MM/DD/YYYY") <= 0)


the YYYY-MM-DD value assigned by the drop-down calendar will automatically be formatted (transformed) into the MM/DD/YYYY format and the Date2Num function will succeed.



The last thing to do is ensure that the field's value is always stored in the MM/DD/YYYY format. You can do this simply by assigning the formatted value to the raw value when the Date2Num function succeeds:



$.rawValue = $.formattedValue


I've attached a second sample form (second version of the first one) which has the fix for using dates selected from the drop-down calendar. It also contains a slight adjustment to make sure that the script doesn't run when the field's value is empty (null):



if ($.isNull ne 1)


This avoids having the error message be displayed when the user simply tabs/clicks in and out of the date field without entering/picking a value.



Finally, this fix has the added benefit of fixing-up the value entered by the user if it doesn't entirely match the MM/DD/YYYY pattern where if the user enters "6/28/06", the value will automatically be adjusted to "06/28/2006". Entering "6-28-06", however, will still fail and cause the error message to be displayed.



Stefan

Adobe Systems

Avatar

Former Community Member
Really appreciated, it works perfectly,

One stranger thing that I have faced is that in the Numeric Fields when the Final Sum is more than 100 if you click on Enter button rather than Tab button, you get the error message but it lets you out the filed without correcting the number. But if you click on Tab you get the error message and get focus back to the field till you correct the entry.

Why is that?

Thanks again

Maryam

Avatar

Former Community Member
One more thing is that:

I have ten Numeric Fields which the total of enetered numbers should not exceed 100. These fields have a script on Exit:



if ((form1.#subform[0].TableOptionalfinancialProducts.Row4[17].CPCTotalPercent.rawValue) <=100) then

// good

$.rawValue = $.rawValue;

else

xfa.host.messageBox("More than 100%. Please change the percent.");

xfa.host.setFocus($.name);

endif



Entering first invalid data puts me in a loop with the SetFocus and the error message.

What could be the solution?



Thanks

Maryam

Avatar

Former Community Member
In reference to your question about the differences between pressing the
Enter and
Tab keys:



I can't say that I have a definite answer for this difference in behaviour since it's not documented very well in the Acrobat Help Topics, but it's probably because pressing the
Enter key simply commits the entered value to the field but doesn't attempt to move the cursor to the next field. Pressing the
Tab key, however, not only commits the entered value but also attempts to move the cursor to the next field.



Stefan

Adobe Systems

Avatar

Former Community Member
Thanks,

What about the codes and the loop I get with this code?



if ((form1.#subform[0].TableOptionalfinancialProducts.Row4[17].CPCTotalPercent.rawValue) <=100) then

// good $.rawValue = $.rawValue;

else

xfa.host.messageBox("More than 100%. Please change the percent."); xfa.host.setFocus($.name); endif



Maryam

Avatar

Former Community Member
Unfortunately, I'm having a difficult time trying to reproduce the problem you're experiencing and therefore can't suggest any fixes.



I've attached a sample form which has similar code that checks for a value entered in the first numeric field. If that value exceeds 100, it displays an error message using xfa.host.messageBox and then sets focus back onto the first numeric field using xfa.host.setFocus.



In order to determine where things are going wrong, I would have to look at your form. If that's OK with you, please send it to formbldr@adobe.com with an email titled, "Percent validation".



Stefan

Adobe Systems