How to manipulate phone numbers | XM Community
Skip to main content

Dear Qualtrics community,
I'm working on a survey that has collected phone numbers from respondents, all validated as US numbers. We're preparing to distribute the next survey in our longitudinal study, and have learned that the country code is required for SMS distributions.
I have seen a few options for adding a country code selector to the question using JS. I'm not opposed to that, but it would be simpler for our respondents if we could add it on their behalf. All respondents are in the US. Hoping that a JS expert might advise on how to add a leading one to submitted phone numbers and store them as embedded data.
Thank you!
Kirsten

Hi kirsten_thompson,
Assuming you're just adding "1" to the start of each number, you could do something like this:
//put this in the onready function for your question
jQuery("#"+this.questionId+" .InputText").on('blur',function(){
Qualtrics.SurveyEngine.setEmbeddedData('Phone', "1" + jQuery(this).val());
});
Whenever the user clicks off of the field, it will update the "Phone" embedded field with the inputted value with a 1 at the start.
Good luck!


Thanks, bgooldfed! This question is set up as a form field with two text boxes, one for phone (QID182 Value 1) and one for email (QID182 Value 2). Would this JS add a 1 in front of the email address, as well?


https://community.qualtrics.com/XMcommunity/discussion/comment/49049#Comment_49049It would, and it would store the email as the Phone variable too. Not what we want, so you'll need to make the selector a little more specific.
Assuming the phone field is the first one in the list:
jQuery("#"+this.questionId+" .InputText").eq(0)on('blur',function(){
Qualtrics.SurveyEngine.setEmbeddedData('Phone', "1" + jQuery(this).val());
});
Otherwise you can increase the '0' in 'eq(0)' to select further down the list (indexes start at 0 in JS, hence the first element is at 0).
Hope that helps!


You don't need JS. You can do it in the survey flow after the phone question:
Set embedded data:
Phone = 1${q://QID82/ChoiceTextEntryValue/1}


Thanks TomG! I didn't realize it was so easy to add text to ED fields...
This worked with a slight modification. Because some of our respondents have entered numbers as XXXXXXXXXX and some as XXX-XXX-XXXX, we had to append 1-. The hyphen means all numbers will be in an acceptable format for SMS distributions.


Leave a Reply