You are viewing limited content. For full access, please sign in.

Question

Question

How to get current user's initials

asked on October 11, 2020 Show version history

I know how to obtain the current user's full name as a variable, but is there a formula I can use to get their initials for a form that just requires initials instead of their full name?

0 0

Replies

replied on October 12, 2020

Depending on how you define initials, this JavaScript would work. It extracts all the uppercase letters from a string and returns them as a new string. 

function getInitials(fullName) {
    const nameArr = fullName.split('');
    let initials = nameArr.filter(function(char) {
        return /[A-Z]/.test(char);
    })
    return initials.join('')
}

This just pulls all the capital letters out of a string. Notice how it behaves for names that have Jr or a Roman numeral.

If you need it to be more robust I found an answer on Stack Overflow which checks for spaces as the delineation between new initials: https://stackoverflow.com/questions/33076177/getting-name-initials-using-js

0 0
replied on October 12, 2020

I am kind of a noob at Javascript....where would I insert this in the Laserfiche form?

0 0
replied on October 12, 2020 Show version history

I'm not sure how much more help I can offer, since it really depends on your form, how you need it, when you need the field to populate, etc.

If you just need a field to populate with initials on load, I will usually populate a separate, hidden field with my source value from Forms (default value is Current User > Name) and use that to run my script against, then populate the actual field I want with the result.

That might be a little outside what you can do as a JS novice since you'd need a good understanding of other JavaScript concepts to make this work in your solution.

Hopefully someone else has a more 'works out of the box' solution that doesn't need as much configuration.

0 0
replied on October 12, 2020

For more context, I just need to fill the current user's first and last name with their initials on the form.  I noticed when adding javascript into Laserfiche it usually beings with:

$(document).ready(function(){

I guess I just need to adapt your Javascript in that format?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.