Skip to main content
Inspiring
March 27, 2017
Answered

Can I Put the "showRequired" Function into the First Half of my IF Statement?

  • March 27, 2017
  • 2 replies
  • 1042 views

Hi,

I have a Validate button on a form that has an IF statement that says, if the required fields have nothing in them, show a message that says to complete the required fields, and it also highlights them using the "showRequired" function.

If the required fields are already filled in, then it shows a message that says all required fields have been filled in.

The showRequired function is being called at the end of the IF statement, which makes the fields get highlighted whether they have all been filled in, or not.

I was just wondering if it is possible to put the showRequired function into the First Half of my IF statement, so that it would highlight them if they have NOT been filled in.

And, then, the code after the "ELSE" would just show the message that everything has been filled in, and it would not highlight the fields.

I have tried to do this, but I keep getting syntax errors and I have tried just about every way of re-doing it.  Not having any luck.

I even tried turning the IF Statement around so that the first half is about if the fields have already been filled in, it gives that message, and then the 2nd half of the statement is when they have NOT been filled in, and so then the showRequired function could just come at the end....  But it can't be outside of the IF Statement, because then everything will highlight whether it's been filled in, Or Not.

Every time I try to include it in the IF statement, I get the syntax errors.

If you could please help me do the code, so that the function is only involved in one half of the IF statement, I would really appreciate it.

Here is my form in dropbox:  Dropbox - generic form_2.pdf

Here is the code:

var oFieldNames = new Object();

function showRequired() {

for ( var i=0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( (f.type != "button") && f.required)  {

oFieldNames[fname]={ strokeColor: f.strokeColor,

fillColor: f.fillColor};

f.strokeColor=color.red;

f.fillColor=app.runtimeHighlightColor;

}

}

}

if (

this.getField("EmpID1").value==""||

this.getField("EmpID2").value==""||

this.getField("EmpID3").value==""||

this.getField("EmpID4").value==""||

this.getField("EmpID5").value==""||

this.getField("EmpID6").value==""||

this.getField("EmpID7").value==""||

this.getField("EmpID8").value==""||

this.getField("EmpLName").value==""||

this.getField("EmpFName").value==""||

this.getField("EmpPhone").value==""||

this.getField("EmpEmail").value==""||

this.getField("ckYes").value=="Off"||

this.getField("EmpSignDate").value==""||

this.getField("EmpTitle").value==""||

this.getField("EmpDept").value==""

)

app.alert({

cMsg: "Please complete all required (*) fields",

nIcon: 1,

cTitle: "Validate Required Fields are Filled"

});

else

app.alert({

cMsg: "Thank you for completing all required (*) fields.",

nIcon: 3,

cTitle: "Validate Required Fields are Filled"

});

showRequired();

Thank you very much.

Correct answer try67

If you execute to include multiple commands after an if-statement they have to be placed within curly brackets. Otherwise just the first command after the condition is "attached" to it.

2 replies

Inspiring
March 27, 2017

I would also use the "valueAsString" property rather than the "value" property since some of the fields might have a number value and null string might not be properly identified. Another option would be to use the "defaultValue" property in case one the fields should have a none null string default value you would not need to recode the function.

I would also not define a "var" within a loop, since this creates a new copy of the variable.

Since you are using the "required" property on the fields, there should be no reason to enumerate the fields again.

Your script could be simplified to:

function showRequired() {
var bPassed = true;
var fname;
var f;
for ( var i=0; i < this.numFields; i++) {
  fname = this.getNthFieldName(i);
  f = this.getField(fname);
  f.strokeColor = color.black;
  f.fillColor = color.white;
  if ( (f.type != "button") && f.required == true && f.value == f.defaultValue)
  {
   f.strokeColor = color.red;
   f.fillColor = app.runtimeHighlightColor;
   bPassed = false;
  } // not button and required;
} // end for loop;
return bPassed;
} // end function showRequired;

if (showRequired() != true)
app.alert({
cMsg: "Please complete all required (*) fields",
nIcon: 1,
cTitle: "Validate Required Fields are Filled"
});
else
app.alert({
cMsg: "Thank you for completing all required (*) fields.",
nIcon: 3,
cTitle: "Validate Required Fields are Filled"
});

Functions can return a result of their processing.

Inspiring
March 28, 2017

Wow ...  thank you for this extra advice.  This should prove very helpful.

Thanks much!  

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 27, 2017

If you execute to include multiple commands after an if-statement they have to be placed within curly brackets. Otherwise just the first command after the condition is "attached" to it.

Inspiring
March 27, 2017

Thank you very much.  I will try this today.  Leaving for work right now.