Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

JavaScript Help / if else statement / regular expression

Avatar

Former Community Member
I have a form where the Client Last Name is bound to a data source. I need to provide a copy of the form to a particular person depending upon which letter of the alphabet the Client Last Name begins with (e.g., Carol gets a copy of forms where the client last name begins with A-G, Karen gets a copy of forms where the client last name begins with H-Pe, etc.). I want the correct persons name to appear in the copy notation of the form. I've tested my regular expressions and they are working correctly. I seem to be having trouble when I get to the else if statement in my script.

Can anyone help with this?



Here is my script:

// Show correct FLU Agent according to Client Last Name

var FLU1 = /(^[A-G])/

var FLU2 = /(^[H-O]|P[a-e])/

var FLU3 = /(^P[f-z]|[Q-Z])/



if (FLU1.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Carol";

}

else if (FLU2.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Karen";

}

else if (FLU3.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Cathy";

}
4 Replies

Avatar

Level 5
In your case all you need to use is just if conditions.... because only one of them will be true any time and no need to use else.



var FLU1 = /(^[A-G])/

var FLU2 = /(^[H-O]|P[a-e])/

var FLU3 = /(^P[f-z]|[Q-Z])/



if (FLU1.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Carol";

}

else if (FLU2.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Karen";

}

else if (FLU3.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Cathy";

}

Avatar

Former Community Member
Thank you for the response:-) I'm using Adobe LiveCycle v8. I removed the two "else if" and replaced with "if", but now it always executes the last line of code, "Cathy" (even though condition is false).

I tested each variable again and only Carol returns true on the Client Last Name I am using.



Does anyone know what else is wrong in my script?



var FLU1 = /(^[A-G])/

var FLU2 = /(^[H-O]|P[a-e])/

var FLU3 = /(^P[f-z]|[Q-Z])/



if (FLU1.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Carol";

}

if (FLU2.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Karen";

}

if (FLU3.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true);

{

this.rawValue = "Cathy";

}

Avatar

Level 5
try the following...removed unwanted semicolon(s)....



var FLU1 = /(^[A-G])/

var FLU2 = /(^[H-O]|P[a-e])/

var FLU3 = /(^P[f-z]|[Q-Z])/



if (FLU1.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true)

{

this.rawValue = "Carol";

}

if (FLU2.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true)

{

this.rawValue = "Karen";

}

if (FLU3.test(xfa.resolveNode("form1.dbObjects.clientLastName").rawValue)== true)

{

this.rawValue = "Cathy";

}

Avatar

Level 7
Have you tried trimming your test string to the number of characters in the RegExp?



// Show correct FLU Agent according to Client Last Name

var FLU1 = /^([A-G][a-z])$/;

var FLU2 = /^([H-O][a-z]|P[a-e])$/;

var FLU3 = /^(P[f-z]|[Q-Z][a-z])$/;



if (HasValue(xfa.resolveNode("form1.dbObjects.clientLastName") {

var NameString = xfa.resolveNode("form1.dbObjects.clientLastName").rawValue.substring(0,2);



switch (true) {



case (FLU1.test(NameString) ):

this.rawValue = "Carol";

break;



case (FLU2.test(NameString) ):

this.rawValue = "Karen";

break;



case (FLU3.test(NameString) ):

this.rawValue = "Cathy";

break;



default:

app.alert("Unknown Last name: " + this.getField("clientLastName").value, 1, 1);

this.rawValue = "";

break;

}// end switch



} else {

this.rawValue ='';

} // end if HasValue



You also might want to consider the 'switch(){}" statement since it skips all following test once a test, case, has been meet and allows for a default action when none of the above tests are met.



I have also added code to prevent running the script when there is no inputted value to the last name.



I have not tested the code for LiveCycle Designer but modified it from AcroForms JavaScript.