6 Replies Latest reply on May 25, 2016 8:23 AM by alexh90310518

    Comparing two numbers using java

    haow84654262

      Hello, pls help.

       

      I have three filed named 'field1', 'field2' and 'field3'.

      And the user will key in a number in 'field1' and after user key in another number in 'field2'.

      The number will automatically compared to the number in 'field1'.

      If the 'field1'>'field2', the system will pop up an alert saying 'field2' must not be less than 'field1'.

      If the 'field2' =< 'field2', the system will run an equation that 'field1/field2*100%' and put this percentage result into 'field3'

       

       

      Can someone help me with the coding Please...

       

      Thank youuu

        • 1. Re: Comparing two numbers using java
          Bernd Alheit Adobe Community Professional & MVP

          In Acrobat you must use Javascript, not Java.

          • 2. Re: Comparing two numbers using java
            alexh90310518 Level 1

            The way I like to do things like this is by first creating a function in your document level scripts.

            The function would look like this;

            function addFields() {
                 var fldT1 = this.getField("Field1").value;
                 var fldT2 = this.getField("Field2").value;
                 var fldT3 = this.getField("Field3");
            
            if (fldT1 > fldT2) {
                 app.alert("Field 1 is greater than Field 2");
            }
            else if (fldT1 < fldT2) {
                 fldT3.value = fldT1/fldT2*100+"%";
            }
            else {
                 fldT3.value = fldT1/fldT2*100+"%";
            }
            }
            

            Then I call this function in both Fields 1 & 2;

            Action: On Blur>Run A JavaScript

            addFields();
            

            I also make sure that the format of the fields 1&2 are set to number so that the other user cannot add a letter or non number character into the field.

            Right-Click Field>Properties>Format>Number

            Make any fields that you don't want the other user to be able to modify/change into read only and you should be ok.

            Hope this helps,

            • 3. Re: Comparing two numbers using java
              haow84654262 Level 1

              First of all, thanks for reply.

               

              What does it mean by creating a function in your document level scripts? which field should I put these coding under in? 1? 2? 3? or all?

              • 4. Re: Comparing two numbers using java
                alexh90310518 Level 1

                In your document you should have Tools>JavaScript>Document JavaScripts, in the menu that shows up from there create a function, call it whatever you like, I generally choose something memorable and self explanatory.

                Then you write the main script into that function, this will set it globally so it can be called from anywhere in the document.

                Once you have defined your function you can call it with;

                functionName();
                

                Eg if you called your function;

                function addFields() {
                     //do something
                }
                

                You call it by typing;

                addFields();
                

                Into the Actions:On blur>Run a JavaScript of your Text fields you want to add together, in this case that would be 'Field1' and 'Field2' there will be no need to call it in 'Field3'

                • 5. Re: Comparing two numbers using java
                  haow84654262 Level 1

                  Thanks!! It works....

                   

                  Just after I filled the field1 and click on field2 before filling, the system has already compared them and the alert poped up. Is there a better way to improve it?

                  Plus, how to limit the decimal places of the percentage in field3?

                  • 6. Re: Comparing two numbers using java
                    alexh90310518 Level 1

                    I'm not sure of any other way of doing it as I've always done it like that, I think you could place it into the Calculation script of Field3 rather than Fields1&2 but I've never had to do that myself so I couldn't help you further there.

                     

                    As for the decimal places you can play around with the settings in the 'Format' Tab to have no decimal places at all.

                     

                    Edit: I changed this section of code inside the function;

                    if (fldT1 > fldT2) {
                         app.alert("Field 1 is greater than Field 2");
                    }
                    

                    To this

                    if (fldT1 > fldT2) {
                         if (fldT2 !=="") {
                              app.alert("Field 1 is greater than Field 2");
                         }
                    }
                    

                    What this does is check if Field1 is greater than Field2 and then if Field2 is not empty.

                    If both of those are true it will display the Alert.

                    This should stop the Alert displaying if Field2 is empty after entering a value into Field1