Jump to content

adding a period to an initial


Cmostaert

Recommended Posts

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

RuleName.png.76f26874fedf221525f2c0bfd3675833.png

Link to comment
Share on other sites

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.');

Link to comment
Share on other sites

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.

Chris

NameRule.pdf

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...