Form level javascript code error

Community Beginner ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Hello!

I have a not fluent in javascript but have come up with this code based on reading numerous forums. I'm not sure what the error in the code is. In this form, the zip code field is empty by default and the county field is set to "Forsyth" by default. The idea is that when the print button is hit, it makes sure that zip code entered is one of the zip codes for the default county.

Here is what I am trying to say: if COUNTY is "Forsyth" and ZIP is anything other than 27101 or 27102, alert an error.

var county = this.getField("CountyDrop").value;

var zip= this.getField("RespZip").value;

if(county == "Forsyth" && (zip != "27101" || zip != "27102"))

  {// Got an error

     app.alert("The zip code you entered is not part of Forsyth county");

  }

I suspect the issue is on line 04 when I try to say 27001 or 27002 because the following code works fine:

if(county == "Forsyth" && zip != "27101")

In this example, I successfully get an error when the county value is Forsyth and the zip code is anything other than 27101. It's just that I need to list about 20 more zip codes.

Thank you!!

TOPICS
Acrobat SDK and JavaScript, Windows

Views

308

Likes

Translate

Translate

Report

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

Engaged , May 24, 2018 May 24, 2018
as gkaiseril​ said, if you need to list all individual zip codes because ranges of values cannot be used, put them all in an array and use the indexOf() method as the validation argument of your if statementvar ZC = ["27101","27102"]if(county == "Forsyth"){  if(ZC.indexOf(zip) == -1)  //returns -1 if element is not in the array  {// Got an error     app.alert("The zip code you entered is not part of Forsyth county");  }}

Likes

Translate

Translate
Adobe Community Professional ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

Use zip != "27101" && zip != "27202"

Likes

Translate

Translate

Report

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
LEGEND ,
May 23, 2018 May 23, 2018

Copy link to clipboard

Copied

It might be because you are logically ORing the value of the zip code to not be 27101 OR 27102 which will always be true because if the zip code is 27101 it will not be equal to 27102 and vice a versa and that combination will always be true and then if the zip code is some other value it will be true. You might want to try a logical AND.

You can always write the values and the results of the test to the JavaScript console and observe what is happening.

Try the following script and observe what is displayed.

var county = this.getField("CountyDrop").value;

var zip= this.getField("RespZip").value;

if(county == "Forsyth" && (zip != "27101" && zip != "27102"))

  {// Got an error

     app.alert("The zip code you entered is not part of Forsyth county");

  }

// some debugging information

console.show();

console.clear();

console.println("County: " + county);

console.println("Zip code: " + zip);

console.println("zip != \"27101\" || zip != \"27102\") " + String(zip != "27101" || zip != "27102"));

console.println("zip != \"27101\" && zip != \"27102\") " + String(zip != "27101" && zip != "27102"));

You can also use ranges of values in your script so you might not have to list all the individual zip codes.

Likes

Translate

Translate

Report

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
Engaged ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

as gkaiseril​ said, if you need to list all individual zip codes because ranges of values cannot be used, put them all in an array and use the indexOf() method as the validation argument of your if statement

var ZC = ["27101","27102"]

if(county == "Forsyth"){

  if(ZC.indexOf(zip) == -1)  //returns -1 if element is not in the array

  {// Got an error

     app.alert("The zip code you entered is not part of Forsyth county");

  }

}

Likes

Translate

Translate

Report

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 Beginner ,
May 26, 2018 May 26, 2018

Copy link to clipboard

Copied

LATEST

Thank you MatLac! This indexOf part is completely new to me, but the code you provided worked perfectly! I really appreciate your help and taking the time to write out (and explain) the code on the other issue as well! It feels great to finally have working code.

Likes

Translate

Translate

Report

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 Beginner ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

Thank you guys!! I figured it was something stupidly simple. In this particular example, the zip codes for the county don't fall exactly within a range so I just listed them all separately. For ease of read below, I removed the entire zip list but here is the code I ended up with which works perfect:

var county = this.getField("CountyDrop").value;

var zip = this.getField("RespZip").value;

if(county == "Forsyth" && (zip != "27009" && zip != "27010" && zip != "27012" ))

  // Got an error  

var nRslt = app.alert("The zip code you entered, " + zip + ", is not part of Forsyth county.\n\n Are you sure you want to continue?" ,2,2);

if(nRslt == 4)

{  // Code printing goes here

   console.println("The response was yes")

}

  else if(nRslt == 3)

{ // No Response

  console.println("The Response Was No");

}

else  

{ //Unknown Response

  console.println("The Response Was somthing other than Yes/No: " + nRslt);

}

I have a print button at the end of this form. Before it prints, I want it to check both for missing fields and the zip/county validation (above). In both cases, I want the user to have the ability to override the warning and print anyway. Here is my code checking for missing fields.

  var patternEmpty = /^\s*$/;

  var strMissing = "";

  if(patternEmpty.test(this.getField("PetName").value))

     strMissing = "petitioner's name";

  else if(patternEmpty.test(this.getField("BIRTHDATE").value))

     strMissing = "patient's date of birth";

  else if(patternEmpty.test(this.getField("NAME OF RESPONDENT").value))

     strMissing = "patient's name";

  else if(patternEmpty.test(this.getField("RespAddr1").value))

     strMissing = "patient's full address";

  else if(patternEmpty.test(this.getField("RespCity").value))

     strMissing = "patient's full address (city)";

  else if(patternEmpty.test(this.getField("RespState").value))

     strMissing = "patient's full address (state)"

  else if(patternEmpty.test(this.getField("Assessment").value))

     strMissing = "assessment section"

  else if(patternEmpty.test(this.getField("SEX").value))

     strMissing = "patient's sex";

  else if(patternEmpty.test(this.getField("RACE").value))

     strMissing = "patient's race";

  else if(patternEmpty.test(this.getField("at").value))

     strMissing = "time of exam";

  else if(patternEmpty.test(this.getField("ImpressionDiagnosis").value))

     strMissing = "patient's diagnosis";

   else

    {// Form must have been completed

console.println("All fields completed"); 

var pp = this.getPrintParams();

pp.interactive = pp.constants.interactionLevel.full;

pp.printRange=[[1,4],[10,10]]

this.print(pp);

  }

  if(strMissing.length)

  {// Got an error

     var nRtn = app.alert("The " + strMissing + " is missing.\n\n Are you sure you want to print it anyway?" ,2,2);

  }

if(nRtn == 4)

{// A yes Response

  var pp = this.getPrintParams();

pp.interactive = pp.constants.interactionLevel.full;

pp.printRange=[[1,4],[10,10]]

this.print(pp);

  console.println("The Response Was Yes - print anyway");

}

else if(nRtn == 3)

{ // No Response

  console.println("The Response Was No");

}

else  

{ //Unknown Response

  console.println("The Response Was somthing other than Yes/No: " + nRtn);

}

else  

{ //Unknown Response

  console.println("The Response Was somthing other than Yes/No: " + nRtn);

}

There is something not quite right about the code above because sometimes (and only sometimes), when the form is submitted for printing, it tries to print it twice. Meaning, I can cancel out of the print box, but it pops right back up. Obviously, it's moving down the script and at the end, the console shows:

All fields completed

The Response Was Yes - print anyway

Can you guys please help me identify the error in the code above? I have spent hours looking through forums and Javascript for Acrobat API Reference without luck. Also, I have one idea but I also haven't seen any examples of how to combine these two codes so that they are both checked and may both be overridden to final goal of print. I really appreciate the help! I have struggled through over 1000 lines of javascript in the entire document and this is the very last issue, haha.

Likes

Translate

Translate

Report

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
Engaged ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

There is a lot of things in there.

First, line 53 to 60 are twice the same thing.

You got 2 print commands, one at line 31 that is called every time, and one at line 46 when the user answers yes to the prompt.

You update strMissing each time a field is empty, losing the information about the previous empty field.

The correct way to implement this is the following:

-Go ahead with the tests just as you did using "if" for each line, not "else if"

-Change this:  strMissing += strMissing+"\npetitioner's name";

it will create some sort of list by appending each empty field at the end of the string.  the \n is a carriage return.  The user will have all the missing fields in view instead pf the last one.

For the last part, start off by creating your print params

then:

if (strMissing.lenght){

   //Here you put all the prompting behavior to override the tests (ajustements to strMissing, the app.alert, etc)

}

else{

   //Here goes the behavior when all test pass

}

Finally, to make it a lot shorter, you should place each field name in an array and loop your test trought that array.  In that loop, you update strMissing.  If you ever want to remove or add a field to test, you just update the array which saves a lot of time.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

When you get the value of the zip code field, use valueAsString and not value. It may not make a difference in this specific case, but some zip codes have a leading zero, and using the value property will result in the variable "zip" being a number (e.g., the number 1234, not the string "01234"). Whenever comparing a field value to a string, use the valueAsString property to avoid the chance of this type of bug.

Likes

Translate

Translate

Report

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 Beginner ,
May 26, 2018 May 26, 2018

Copy link to clipboard

Copied

Thanks, George! That's a great point. I updated the code to utilize valueAsString. Might save me a headache down the road!

Likes

Translate

Translate

Report

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