Forum Moderators: open
Basically, I have a PDF order form (don't ask).
I am using JavaScript in the PDF to check that certain items with minimum quantities are meeting said minimums. If the user enters a quantity below the minimum, the entry field turns red and the quantity is automatically re-set to the minimum value. If the user enters nothing, the field is left blank.
With me so far? Here is the code from the PDF form:
var d = this.getField("q12");
if (d.value >= 1 && d.value < 100)
{
this.getField("q12").textColor = color.white;
this.getField("q12").fillColor = color.red;
event.value = 100;
}
else {
event.value = d.value;
this.getField("q12").textColor = color.black;
this.getField("q12").fillColor = color.transparent;
}
This works ok, except if the user enters a negative number (because I am only checking for numbers from 1 to 100). I know that I can change negative numbers into positive with
Math.abs(d.value), but I don't know where to insert this into the above code. Any help would be much appreciated.
var d = Math.abs(this.getField("q12")); - The values are strings, but Math.abs will convert them to number.
..but this means that you will accept negative input! Do you want this?
According to your description, you shouldn't do that.
These lines bother me:
event.value = [red]100[/red];
...
event.value = [red]d.value[/red]; Shouldn't they be?:
event.value = [blue]d[/blue]; <-- value received
...
event.value = [blue]1[/blue]; <-- minimum
Math.abs at the beginning as you have suggested, I get the following error message: uncaught exception:Field q12:Calculate:9: Invalid or unknown Event.value, set not possible. As far as the values I am using, I tried those changes as well, and while it accepted the changes, it no longer worked as I wanted it to.
It is not my intent to accept negative numbers - what I am trying to do is automatically change any negative values into positive ones, and still check to see if the number meets the minimum.
Here is a breakdown of what is happening, as I understand it so far. Maybe it will help.
var d = this.getField("q12");
if (d.value >= 1 && d.value < 100)
{
this.getField("q12").textColor = color.white;
this.getField("q12").fillColor = color.red;
event.value = 100;
}
else {
event.value = d.value;
this.getField("q12").textColor = color.black;
this.getField("q12").fillColor = color.transparent;
}
And, if the user leaves the field alone, or enters "0" then nothing happens at all.
I want to add to this the ability to recognize when a negative number has been entered, and disallow it by automatically changing it to its absolute value, and then executing the rest of the script.
Maybe it's too much trouble for a PDF?
Try this:
-------------------------------------
var field = this.getField("q12");
var val = Math.abs(field.value);if (val >= 1 && val < 100)
{
field.textColor = color.white;
field.fillColor = color.red;
event.value = 1; // "..is overridden to the minimum allowed"
}
else
{
field.textColor = color.black;
field.fillColor = color.transparent;
event.value = val;
}
-------------------------------------
var d = this.getField("q12");
Here we are getting the value entered into the field "q12,"
I'm guessing that it actually returns the field itself. Not the value.
...which will always be a number, since the field is already configured for numbers only via the PDF form tool.
Maybe the values they contain digits, but they are still, I dare say, of type:
string. The Math.abs will take care of that. In JS comparisons between numbers & strings that look like numbers will still give the right result anyway. Worth keeping in mind though.
Okay, that almost works as I want it to, there's just one more thing I want it to do, and that is to allow the field to remain empty. With the changes you suggested (thank you very much!) it now defaults to a "0" for that field. A small complaint I know, but I want it to be consistent with the other entry fields in the form (the ones that don't have minimums assigned).
Thanks for all the help just the same - I feel I learned a lot.