Skip to main content
M.Hasanin
Inspiring
May 24, 2016
Answered

Multiple if conditions

  • May 24, 2016
  • 2 replies
  • 4335 views

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;

}

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

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 "}"

2 replies

M.Hasanin
M.HasaninAuthor
Inspiring
May 26, 2016

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

Mohammad Hasanin
Karl Heinz  Kremer
Community Expert
Community Expert
May 26, 2016

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.

M.Hasanin
M.HasaninAuthor
Inspiring
May 26, 2016

Brilliant Idea!

Mohammad Hasanin
Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
May 24, 2016

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 "}"