Scriptlet: Format Phone Value

 

//returns xxx-xxx-xxxx format
function ConvertPhone(phone) {
var formatted = phone.replace(/[^0-9]+/g, '');
if (formatted.length == 10){
formatted = formatted.replace(/^[2-9]\d{2}-\d{3}-\d{4}$, '$1-$2-$3'/);
}
else {
formatted = respondent.phone;
}
return formatted;
}