How to manipulate text of Embedded Data | XM Community
Skip to main content

I have a text entry question wherein I ask participants "What is your first name?"
I want to take their answer and store it as an embedded data field.
If someone inputs their first name (e.g., "Ayanna" or "Will"), things work perfectly! I use Survey Flow to set embedded data from their input.
The problem is that some people are inputting their names badly:

  • Not capitalizing the first initial: "joe"

  • Capitalizing every letter: "JOE"

  • Including a last initial: "Joe T" or "Joe T."

  • Including a last initial as one word: "JoeT"

  • Weird typos: ", Joe"

Question: How do I take people's text input and standardize the format--capitalized first letter, just 1 word, nothing extraneous--and save it as an embedded data?
I think this will somehow require JavaScript, but I'm not sure (a) how to do this (I think it involves regular expressions? Qualtrics.Survey.Engine.set.embedded.data?), or even (b) where I would implement this (survey flow? question javascript?).
*I'm so grateful for any guidance you might have!*

LE: My idea is a workaround: it doesn't manipulate the text that participants enter when asked their name. Instead, it only allows them to enter text in a specified format.
I think what you need is a Custom Validation (under Validation Types) for the first name entries, and use Matches Regex. This might be of help: https://stackoverflow.com/questions/50048342/html-regex-first-letter-to-be-capitalized
I have just tried setting Matches Regex equal to "[A-Z][a-z]*" (without the inverted commas), and there still seems to be an error, even though the rule seems to work just fine here: https://regex101.com/r/cO8lqs/14
Good luck!


Clever idea; thanks for trying here! Unfortunately, I need to manipulate the string they enter. If a participant enters a badly formatted name, I want to reformat it myself rather than alerting them to the fact that they entered it incorrectly. (e.g., I don't want "joe" to know that I've taken his name to be "Joe".)

Any idea how I might reformat using regex?


Case 4 is more complicated, because you are assuming that you know their first name. What if someone's first name is Joet? If you have a list, then you can use filters to apply to remove those names.
For everything else, you can use this snippet to clean the names and then store them into an embedded variable:
var typed_name = "${qid.....}"; // Get name from the question

// Split at any character which is not a letter
typed_name = typed_name.split(/^a-zA-Z]/gm);
for (i = 0; i < typed_name.length; i++) {
    if (typed_name+i].length >= 3) {
        break;
    }
}
//Take first name as first string that is longer than 3 chars
typed_name = typed_nameti];
// Capitalize the first letter
typed_name = typed_name.charAt(0).toUpperCase() + typed_name.toLowerCase().slice(1);

Qualtrics.SurveyEngine.setEmbeddedData("first_name", typed_name);


This gets added to the OnLoad JS of the question?
Also, for someone whose name is "Mo," what will happen? Can I just change every instance of 3 in your code to 2 in order to accommodate the Mo's of the world?


This will need to go into a question after they have entered their name. Either in OnLoad or Ready, it doesn't matter. There should just be one page break between asking the name and this code. Also, before using the embedded variable "first_name", you'll need a page break.
Yes. You can just change 3 to 2 for the Mos of the world.


Fantastic, once again ahmedA, your helpfulness and skill amaze.
Thank you so much!


Leave a Reply