Javascript to show only last 4 of number | XM Community
Skip to main content

Hi all,
I am looking for some custom Javascript that will take a phone number from an embedded data field and show only the last four digits, converting the others to asterisks. I have phone number as an embedded data field ("phone1") but, for privacy purposes, am looking for something that will (for example) take the number 1119991234 from the embedded field and show the following:
"Can you confirm that your current phone number is ******1234?"
Thanks!

Use this script:
     $('#NextButton').click(function () {
var textEntry ="";
var replaceText="*";
var x=textEntry.replace(/\\d(?=\\d{4})/g,replaceText);
Qualtrics.SurveyEngine.setEmbeddedData('vMasked',x);
});

declare vMasked embedded variable before the question where this script is added.


Thanks Appzk.
In order for this to work, I believe the code has to appear in a question block that appears prior to where the text will be displayed.
My survey is a single page, is there a way to write and display the embedded variable on the same page?


Add this script in a descriptive type of question before your page and add a page break.
Auto advance this question.
Keep both of this in one block.


Leave a Reply