Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Multiple if conditions

Enthusiast ,
May 24, 2016 May 24, 2016

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;

}

Best
Mohammad Hasanin
TOPICS
Acrobat SDK and JavaScript , Windows
4.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 24, 2016 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

...
Translate
Community Expert ,
May 24, 2016 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 "}"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2016 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

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2016 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2016 May 26, 2016

Brilliant Idea!

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2016 May 26, 2016

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!

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2016 May 26, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2016 May 26, 2016
LATEST

Thanks for your patience , thanks a lot for your help

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 26, 2016 May 26, 2016

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

Best
Mohammad Hasanin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines