Forum Moderators: open

Message Too Old, No Replies

Check Absolute Value of a Number

Newbie would like to know …

         

katana_one

8:15 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



I'm not well versed in JavaScript at all, and am surprised I have gotten myself as far as I have with my current project.

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.

orion_rus

8:28 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



make it after var d, and i think all will be fine)

katana_one

8:31 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



I tried that, and it didn't work. Maybe I am not understanding you correctly. Can you be more specific?

Bernard Marx

10:28 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just like orion says:

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

katana_one

5:18 am on Dec 10, 2004 (gmt 0)

10+ Year Member



When I put the
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");

Here we are getting the value entered into the field "q12," which will always be a number, since the field is already configured for numbers only via the PDF form tool.

if (d.value >= 1 && d.value < 100)
{
this.getField("q12").textColor = color.white;
this.getField("q12").fillColor = color.red;
event.value = 100;
}

Here we check to see if the number is between 1 and 99, and if it is, we change the field color to red, and the number is overridden to the minimum allowed.

else {
event.value = d.value;
this.getField("q12").textColor = color.black;
this.getField("q12").fillColor = color.transparent;
}

If the number is not between 1 and 99, we keep the value, and set the field's colors back to normal.

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?

Bernard Marx

9:35 am on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now I think I understand. I thought that getField returns a value, it doesn't, it returns the field (duh?). Not used to the PDF API - but that wasn't so hard. No excuse for the later confusion.

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.

katana_one

1:39 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



Ah, so I see you have added another variable. I tried that, but I guess I just didn't get the proper syntax. Did I mention this is Adobe's particular brand of JavaScript for PDFs?

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).

katana_one

7:57 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



After much tinkering, I finally opted to create an additional condition that checks to see if the number is below 0, and if it is it resets the number to 0. Figuring in the absolute value was just silliness on my part - I realized I was over-complicating things. I tend to do that when trying to learn something new …

Thanks for all the help just the same - I feel I learned a lot.