Scriptlet: Format Phone Value

Convert phone number value on form to xxx-xxx-xxxx format.

//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; 
}