There are several ways.
I suggest you look for existing questions/answers.
A form in code:
- separate the string into specific fields.
.filter('telefone', function() {
/*
Formata numero do telefone como: (xx) xxxx-xxxx ou (xx) xxxxx-xxxx
*/
return function (number) {
/*
@param {Number | String} number - Number that will be formatted as telephone number
Returns formatted number: (##) ####-#### or (##) #####-####
if number.length < 4: ###
else if number.length < 7: (###) ###
*/
if (!number) { return ''; }
number = String(number);
// Will return formattedNumber.
// If phonenumber isn't longer than an area code, just show number
var formattedNumber = number;
// if the first character is '1', strip it out and add it back
var area, front, end;
if (number.length == 10){
// # (##) ####-#### as c (area) front-end
area = number.substring(0,2);
front = number.substring(2, 6);
end = number.substring(6, 10);
}
else
{
// # (##) #####-#### as c (area) front-end
area = number.substring(0,2);
front = number.substring(2, 7);
end = number.substring(7, 11);
}
if (front) {
formattedNumber = ( "(" + area + ") " + front);
}
if (end) {
formattedNumber += ("-" + end);
}
return formattedNumber;
};
})