8 Replies Latest reply on May 26, 2016 12:08 PM by medos20

    Multiple if conditions

    medos20 Level 1

      Hi experts..

       

      Im trying to make multiple if conditions in acrobat javascript, so my problem is easy i want to alert the user and then change the value of field if the user enter the wrong number.. so my approach was the following..but it give me errors (in custom validation scripts for the field)

      switch (event.value) {
      case "0":

      this.getField("PeriodVar").value = 20;

      app.alert("zero not accepted");

      break;  
      case "":

      this.getField("PeriodVar").value = 20;

      app.alert("empty not accepted");

      break;  
      // case > 20:

      //   this.getField("PeriodVar").value = 20;

      app.alert("over 20 not accepted");

      // break;
      }

       

       

      i tried to make multiple if condition with else but also i faild, i only success in the following code as a function :
      function checkmaxperval()
      {
              var maxsum = getField("PeriodVar");
              var usingsum = maxsum.value;
                 if (usingsum > 20 ) {
                      app.alert("maximum value is 20 only", 3);  
                      maxsum.value = 20;
                     return;
      }
      
        • 1. Re: Multiple if conditions
          Karl Heinz Kremer Adobe Community Professional

          It looks like you were trying to have a "case > 20" - this is not valid JavaScript. Every case needs to be one distinct value.

           

          You are better of doing this in an if/else if/else statement:

           

          if (event.value == 0) {
            // do someting
          }
          else if (event.value == "") {
          // do something else
          }
          else if (event.value > 20) {
          // do something else
          }
          else {
          // this catches anything that was not yet handled
          }
          

           

          Having said that, I would advice against checking for an empty string: The problem with this is that you cannot easily remove a value and then replace it with valid data, or leave the field empty and move on to something else to come back later. I would suggest to handle these cases later, e.g. when the form is submitted: Have a function that checks for "required" fields that are empty. See for example here for code that would do that: Loop Through Required Fields before Submitting Form (JavaScript)

           

          Your second code snippet is missing a "}"

          1 person found this helpful
          • 2. Re: Multiple if conditions
            medos20 Level 1

            Thank you Eng.Karl, the problem is I am  working on complicated form represent Table that record trainee present and absent so the user must enter the value immediately not later so the calculation of the percentage of presence will work accurately, i succeeded to make this working, i figured the problem Acrobat Javascript remark (0) or ("") are equal in most cases, but you need only to check the EMPTY case alone with separated Script that will be called from the percentage field , i don't know why but its work great now, thanks for your help

            • 3. Re: Multiple if conditions
              Karl Heinz Kremer Adobe Community Professional

              As long as your users are OK with this, you can do whatever you want In a closed group (e.g. timesheets for employees), as long as you can train your users, that is fine. When you develop forms for a large (and unknown) audience, you have to be a lot more careful about how the user experiences a form, and oftentimes, when they get stuck in a field (but are not yet ready to provide that information), you end up with frustration and angry calls to somebody who has to answer them.

               

              Yes, JavaScript treats both 0 and "" the same, but you can work around that by using the Field.valueAsString property instead of Field.value - this way, if the field contains 0, you end up with a string of "0" and that makes it easy to distinguish from an empty string.

              • 4. Re: Multiple if conditions
                medos20 Level 1

                Brilliant Idea!

                • 5. Re: Multiple if conditions
                  medos20 Level 1

                  But what if the user , lets say enter a String not empty ? how i will avoid this value? because if i enter any value rather than number the all percentage field for all trainee's continue to give me alert error!

                  • 6. Re: Multiple if conditions
                    Karl Heinz Kremer Adobe Community Professional

                    One way to validate if a given string is a number is by using the isNaN() function ("Is Not A Number"):

                     

                    var someVariable = "Test";

                    if (isNaN(someVariable)) {

                       app.alert("The value " + someVariable + " is not a valid number");

                    }

                     

                    This allow you to check if the data you've received is a valid number of not.

                    1 person found this helpful
                    • 7. Re: Multiple if conditions
                      medos20 Level 1

                      i found the solution, i made the field only accept the numbers,  

                      • 8. Re: Multiple if conditions
                        medos20 Level 1

                        Thanks for your patience , thanks a lot for your help