Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

JavaScript Help / if else statement / regular expression

Avatar

Avatar
Boost 1
Level 2
pmcalex
Level 2

Like

1 like

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
pmcalex
Level 2

Like

1 like

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
pmcalex
Level 2

09-06-2008

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";

}

Replies

Avatar

Avatar
Level 5
_SekharN_
Level 5

Likes

0 likes

Total Posts

246 posts

Correct Reply

0 solutions
View profile

Avatar
Level 5
_SekharN_
Level 5

Likes

0 likes

Total Posts

246 posts

Correct Reply

0 solutions
View profile
_SekharN_
Level 5

09-06-2008

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

Avatar
Boost 1
Level 2
pmcalex
Level 2

Like

1 like

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
pmcalex
Level 2

Like

1 like

Total Posts

27 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
pmcalex
Level 2

09-06-2008

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

Avatar
Level 5
_SekharN_
Level 5

Likes

0 likes

Total Posts

246 posts

Correct Reply

0 solutions
View profile

Avatar
Level 5
_SekharN_
Level 5

Likes

0 likes

Total Posts

246 posts

Correct Reply

0 solutions
View profile
_SekharN_
Level 5

10-06-2008

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

Avatar
Boost 5
Level 7
gkaiseril
Level 7

Likes

13 likes

Total Posts

456 posts

Correct Reply

2 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Applaud 5
View profile

Avatar
Boost 5
Level 7
gkaiseril
Level 7

Likes

13 likes

Total Posts

456 posts

Correct Reply

2 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Applaud 5
View profile
gkaiseril
Level 7

10-06-2008

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.