Skip to main content
Participant
March 30, 2023
Answered

Javascript with "OR" statement - PDF Form

  • March 30, 2023
  • 1 reply
  • 2550 views

I have two form fields setup (one labeled "Zip" and the other labeled "Name") to where a user would type in their Zip code and the name field would populate with the sales person that covers that territory. Each sales person covers multiple territories so I want to list out each of their assigned zip codes using "or" syntax (||). However, the issue I'm running into is no matter what zip code I enter, it's only showing the last sales rep. Below is the code I'm entering inside the custom validation script for two sales reps and no matter what I enter, it populates Jane Doe. 

 

This is relatively new to me and I'm sure I'm missing something. Thank you!

 

if (event.value=="64720"||"64730"||"64744"||"64759"||"64772"||"64801"||"64804"||"64831"||"64834"||"64836"||"64850"||"64870"||"65049"||"65065"||"65536"||"65583"||"65605"||"65613"||"65616"||"65622"||"65625"||"65672"||"65686"||"65706"||"65708"||"65714"||"65721"||"65737"||"65738"||"65781"||"65802"||"65804"||"65806"||"65807"||"65809"||"65810"||"71602"||"71603"||"71655"||"71701"||"71730"||"71742"||"71753"||"71801"||"71852"||"71854"||"71901"||"71909"||"71913"||"71923"||"71953"||"72012"||"72015"||"72019"||"72022"||"72023"||"72031"||"72032"||"72034"||"72076"||"72088"||"72104"||"72110"||"72112"||"72113"||"72114"||"72116"||"72120"||"72143"||"72150"||"72160"||"72201"||"72205"||"72207"||"72209"||"72211"||"72212"||"72223"||"72315"||"72364"||"72396"||"72401"||"72405"||"72450"||"72455"||"72501"||"72529"||"72543"||"72560"||"72601"||"72632"||"72653"||"72701"||"72703"||"72704"||"72712"||"72714"||"72715"||"72736"||"72740"||"72756"||"72758"||"72761"||"72762"||"72764"||"72801"||"72802"||"72830"||"72855"||"72901"||"72903"||"72908"||"72916"||"72921"||"72923"||"72927"||"72956"||"74003"||"74006"||"74010"||"74011"||"74012"||"74014"||"74015"||"74017"||"74019"||"74020"||"74021"||"74023"||"74037"||"74055"||"74063"||"74066"||"74070"||"74105"||"74114"||"74119"||"74133"||"74135"||"74136"||"74137"||"74145"||"74301"||"74344"||"74354"||"74361"||"74401"||"74403"||"74426"||"74429"||"74434"||"74437"||"74447"||"74464"||"74467"||"74501"||"74525"||"74601"||"74728"||"74745"||"74804"||"74820"||"74834"||"74848"||"74864"||"74868"||"74953"||"74955"||"75501"||"75503"||"75551"||"75570") this.getField("Name").value = "#01 - John Doe";
if (event.value=="46001"||"46012"||"46013"||"46016"||"46032"||"46033"||"46036"||"46037"||"46038"||"46040"||"46041"||"46052"||"46055"||"46060"||"46064"||"46072"||"46074"||"46077"||"46107"||"46112"||"46121"||"46122"||"46123"||"46131"||"46140"||"46142"||"46143"||"46151"||"46154"||"46158"||"46163"||"46168"||"46173"||"46176"||"46205"||"46214"||"46217"||"46219"||"46220"||"46227"||"46229"||"46236"||"46237"||"46239"||"46240"||"46250"||"46256"||"46259"||"46260"||"46268"||"46290"||"46310"||"46392"||"46507"||"46514"||"46516"||"46517"||"46526"||"46528"||"46530"||"46540"||"46544"||"46545"||"46550"||"46561"||"46565"||"46567"||"46580"||"46581"||"46582"||"46590"||"46601"||"46614"||"46615"||"46617"||"46628"||"46635"||"46703"||"46706"||"46714"||"46725"||"46733"||"46737"||"46738"||"46741"||"46750"||"46755"||"46761"||"46765"||"46767"||"46774"||"46777"||"46783"||"46802"||"46804"||"46805"||"46809"||"46814"||"46815"||"46818"||"46825"||"46835"||"46845"||"46901"||"46902"||"46923"||"46933"||"46947"||"46952"||"46953"||"46962"||"46970"||"46979"||"46992"||"47001"||"47006"||"47025"||"47042"||"47111"||"47112"||"47119"||"47122"||"47129"||"47130"||"47150"||"47167"||"47170"||"47172"||"47220"||"47240"||"47250"||"47265"||"47274"||"47304"||"47305"||"47327"||"47330"||"47331"||"47340"||"47346"||"47348"||"47359"||"47362"||"47371"||"47374"||"47394"||"47396"||"47421"||"47454"||"47901"||"47904"||"47905"||"47906"||"47909"||"47918"||"47944"||"47946"||"47960"||"47978"||"49002"||"49010"||"49015"||"49047"||"49068"||"49079"||"49085"||"49103"||"49112"||"49120") this.getField("Name").value = "#2 - Jane Doe";

This topic has been closed for replies.
Correct answer Nesa Nurani

You can save some space by apply 'event.value' to a variable (see my example script).

If the value is not correct, you can use app.alert to pop up and alert the user that a wrong value is entered, something like this:

var e = event.value;
if(event.value){
 if(e=="64720"||e=="64730"||e=="64744")
 this.getField("Name").value = "#01 - John Doe";
 else if(e=="46001"||e=="46012"||e=="46013")
 this.getField("Name").value = "#2 - Jane Doe";
 else{
 event.rc = false;
 app.alert("Your message goes here",3);}}
else
this.getField("Name").value = "";

 

1 reply

Nesa Nurani
Community Expert
Community Expert
March 30, 2023

You can't compare it like that, you need to use event.value for each zip code:

(event.value=="64720"|| event.value == "64730"|| event.value == "64744"...etc

I would suggest you to put zip codes in an array and then compare value with array.

tmc414Author
Participant
March 31, 2023

Not familiar with arrays or how to setup but the first part did work. Due to the amount of zip codes, I couldn't fit all of it in the internal editor so had to use notepadd++ but finally getting there so thank you for the quick response as you saved me a lot of time and effort.

 

One last question, what code would I use to account for data that I didn't provide. For example, if somebody enters a zip code where we don't currently have an assigned sales rep. In those instances, I'd want to have a message telling them to call our 800# but what code would I use for something like that?

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 31, 2023

You can save some space by apply 'event.value' to a variable (see my example script).

If the value is not correct, you can use app.alert to pop up and alert the user that a wrong value is entered, something like this:

var e = event.value;
if(event.value){
 if(e=="64720"||e=="64730"||e=="64744")
 this.getField("Name").value = "#01 - John Doe";
 else if(e=="46001"||e=="46012"||e=="46013")
 this.getField("Name").value = "#2 - Jane Doe";
 else{
 event.rc = false;
 app.alert("Your message goes here",3);}}
else
this.getField("Name").value = "";