Good Morning, I have found from this site the coding needed to pull the initials of any given name with two words.
ie. from the word Nova Scotia, the code below pulls out the initials "NS" into the nameabr text field when placed on the exit event of the name field in Javascript.
var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");
for(var i=0;i<wordArray.length;i++)
{
initials += wordArray[i].substring(0,1);
}
Nameabr.rawValue = initials;
now...I would like to change this code a bit so that if there are instances where a name is one word, for example "Alberta", the script would then pull out the first three letters of the word ie. "Alb" rather than just resulting in "A". But I would like to keep the initials if the name is more than one word, ie. revert to the above code.
I assume that there would initially be a count of word
Thanks ahead of time.
J
Solved! Go to Solution.
Views
Replies
Total Likes
BEFORE the for statement.
var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");
if (wordArray.length==1)
initials=wordArray[0].substring(0,3);
else
for(var i=0;i<wordArray.length;i++)
{
initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;
Kyle
Views
Replies
Total Likes
You can put:
if (wordArray.length==1)
initials=wordArray[0].substring(0,3);
else
right before your for statement.
Also, if you are using this for the purpose of changing province names into short form, I would recommend using a list with the official province codes (or you might have just been using that as an example).
Kyle
Views
Replies
Total Likes
Hi dcidev,
Like so? I tried below and the townabr field did not update.
var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");
for(var i=0;i<wordArray.length;i++)
{
if (wordArray.length==1)
initials=wordArray[0].substring(0,3);
else
initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;
J
Views
Replies
Total Likes
BEFORE the for statement.
var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");
if (wordArray.length==1)
initials=wordArray[0].substring(0,3);
else
for(var i=0;i<wordArray.length;i++)
{
initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;
Kyle
Views
Replies
Total Likes
Thank you so much Kyle, I had inadvertently placed this code in the change event and not the exit event at first, but after placing in the right event, it worked!!! amazing!
J
Views
Replies
Total Likes
Views
Likes
Replies