Cmostaert Posted August 21, 2014 Share Posted August 21, 2014 Hi there, We are needing to add a period if a first name or middle name is an initial only. For example: W Kenny Massey should be W. Kenny Massey Or Christine M Mostaert should be Christine M. Mostaert We have a rule that I think has a few things missing on line 19, 20, and 40. I am attaching a screen shot of the rule. I hope it is readable. Any help would be appreciated. Chris Quote Link to comment Share on other sites More sharing options...
step Posted August 21, 2014 Share Posted August 21, 2014 I can't read the screenshot you attached so here's an example of how to do it using the replace function and a regular expression: var s = "W Kenny Massey Christine M Mostaert"; return s.replace(/(\b[a-z]\b)/gi,'$1.'); Quote Link to comment Share on other sites More sharing options...
Cmostaert Posted August 21, 2014 Author Share Posted August 21, 2014 Here is a pdf of the rule we have. The names I used for an example are fake. We have Agents that will be ordering items from our web-to-print system and can choose what combination of their names they want. Some of the agents have an initial for their first name and some have a middle initial only. When they place their order this rule is supposed to add the period when there is that initial. This rule as is returns nothing, no matter what is selected by the person ordering. I think the problem is in line 19, 20 & 40, but I don't know what it needs to be to work. Hope this makes sense. ChrisNameRule.pdf Quote Link to comment Share on other sites More sharing options...
step Posted August 21, 2014 Share Posted August 21, 2014 Is there a reason that you're attaching images of text rather than copying and pasting the code into code tags on the forum? It makes it pretty difficult to work with. Anyway, I think you can shorten the rule a lot to something like this: var firstName = (Field("ChooseFirstName") == "First Name") ? Trim(Field("First Name")) : Trim(Field("Nickname")); var middle = (Field("Middle") == "Yes") ? Trim(Field("MiddleInitial")) : ""; var last = Trim(Field("LastName")); var fullName = [firstName,middle,last].filter(String).join(" ").replace(/(\b[a-z](?!\.)\b)/gi,'$1.'); var result = [fullName,Trim(Field("Suffix")),Trim(Field("Designation"))].filter(String).join(", "); return result; Quote Link to comment Share on other sites More sharing options...
Cmostaert Posted August 22, 2014 Author Share Posted August 22, 2014 Here is the rule I am trying to get to work. var firstNameInitial = Field("FirstName"); var middleNameInitial = Field("MiddleInitial"); if (Field("FirstName").length == 1 && Field("FirstName") != ".") { firstNameInitial = Field("FirstName")+"."; } if (Field("MiddleInitial").length == 1 && Field("MiddleInitial") != ".") { middleNameInitial = Field("MiddleInitial")+"."; } var s = ""; if (Field("Middle") == "Yes") { if (Field("ChooseFirstName") == "First Name") { s = AppendText(s, "", firstNameInitial); s = AppendText(s, " ", middleNameInitial); s = AppendText(s, " ", Field("LastName")); s = AppendText(s, ", ", Field("Suffix")); s = AppendText(s, ", ", Field("Designation")); } else if (Field("ChooseFirstName") == "Nickname") { s = AppendText(s, "", Field("NickName")); s = AppendText(s, " ", Field("LastName")); s = AppendText(s, ", ", Field("Suffix")); s = AppendText(s, ", ", Field("Designation")); } } else if (Field("Middle") == "No") { if (Field("ChooseFirstName") == "First Name") { s = AppendText(s, "", firstNameInitial); s = AppendText(s, " ", Field("LastName")); s = AppendText(s, ", ", Field("Suffix")); s = AppendText(s, ", ", Field("Designation")); } else { s = AppendText(s, "", Field("NickName")); s = AppendText(s, " ", Field("LastName")); s = AppendText(s, ", ", Field("Suffix")); s = AppendText(s, ", ", Field("Designation")); } } return s; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.