Copy link to clipboard
Copied
Hi Everyone
I want my program to detect if a field has an entry and if it has an entry set the output field to a value. Currently the program gets stuck on the if statement.
The following code is in custom calculation script. Running on Adobe Acrobat Pro 2017. Also if there is a easier way to do this please let me know, thanks in advance.
var nameOne = this.getField("Name1").valueAsString;
var nameTwo = this.getField("Name2").valueAsString;
var nameThree = this.getField("Name3").valueAsString;
var nameFour = this.getField("Name4").valueAsString;
var nameFive = this.getField("Name5").valueAsString;
if( nameOne != "null"){
event.value=1;
}
else if(nameOne && nameTwo != "null"){
event.value= 2;
}
else if(nameOne && nameTwo && nameThree != "null"){
event.value= 3;
}
else if(nameOne && nameTwo && nameThree && nameFour != "null"){
event.value= 4;
}
else if(nameOne && nameTwo && nameThree && nameFour && nameFive != "null"){
event.value= 5;
}
You should add something like this at the end of the code:
else event.value = "";
Copy link to clipboard
Copied
The valueAsString property can't return null, only strings. Replace all instances of "null" in your code with "", therefore.
Copy link to clipboard
Copied
Thanks for the reply. I have changed the code to as shown; however, is still returns 1
if( nameOne != ""){
event.value=1;
}
else if(nameOne && nameTwo != ""){
event.value= 2;
}
else if(nameOne && nameTwo && nameThree != ""){
event.value= 3;
}
else if(nameOne && nameTwo && nameThree && nameFour.value != ""){
event.value= 4;
}
else if(nameOne && nameTwo && nameThree && nameFour && nameFive != ""){
event.value= 5;
}
Copy link to clipboard
Copied
You should add something like this at the end of the code:
else event.value = "";
Copy link to clipboard
Copied
I changed my logic around because it was getting stuck on the if because the if was always true because the form always has the name1 entered then name2 ect,.
var nameOne = this.getField("Name1").valueAsString;
var nameTwo = this.getField("Name2").valueAsString;
var nameThree = this.getField("Name3").valueAsString;
var nameFour = this.getField("Name4").valueAsString;
var nameFive = this.getField("Name5").valueAsString;
if( nameOne && nameTwo && nameThree && nameFour && nameFive != ""){
event.value=5;
}
else if(nameOne && nameTwo && nameThree && nameFour != ""){
event.value= 4;
}
else if(nameOne && nameTwo && nameThree){
event.value= 3;
}
else if(nameOne && nameTwo != ""){
event.value= 2;
}
else if(nameOne!= ""){
event.value= 1;
}
else(event.value = " ");
Copy link to clipboard
Copied
You must use:
if( nameOne != "" && nameTwo != "" && nameThree != "" && nameFour != "" && nameFive != ""){
event.value=5;
...
Copy link to clipboard
Copied
Thanks Bernd, I could reverse the change i made
var nameOne = this.getField("Name1").valueAsString;
var nameTwo = this.getField("Name2").valueAsString;
var nameThree = this.getField("Name3").valueAsString;
var nameFour = this.getField("Name4").valueAsString;
var nameFive = this.getField("Name5").valueAsString;
// determine if a string has been entered
if( nameOne!= "" && nameTwo!= "" && nameThree!= "" && nameFour!= "" && nameFive != ""){
event.value=5;
}
else if(nameOne!= "" && nameTwo!= "" && nameThree!= "" && nameFour != ""){
event.value= 4;
}
else if(nameOne!= "" && nameTwo!= "" && nameThree!= ""){
event.value= 3;
}
else if(nameOne!= "" && nameTwo != ""){
event.value= 2;
}
else if(nameOne!= ""){
event.value= 1;
}
else(event.value = " ");