Applying Jquery mask in value and not input

0

Hello, I have the following phone and cell values respectively:

Phone: 1111111111 Cellphone: 11111111111

I already have the value in the database, I would like to present these values formatted with the following masks:

Phone = (11) 1111-1111 Cellphone = (11) 11111-1111

Can anyone help me? I would like to just apply the masks via JavaScript do not want to in the input, as I save the numeric value only in the BD, I would just like to present it formatted.

If anyone can help, thank you!

    
asked by anonymous 25.07.2017 / 23:10

1 answer

2

Use Regex and replace ():

function mascaraTelefone(value){
    value = value.replace(/\D/g,"");                  //Remove tudo o que não é dígito
    value = value.replace(/^(\d{2})(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
    value = value.replace(/(\d)(\d{4})$/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
    value = value.substr(0, 15);
    return value;
}

Call the function by passing the value (phone) the return of it will be the phone formatted:

//Esse seria o telefone que vem do banco de dados
var tel = "1125346283"

//Essa variavel você pode apresentar
var telFormatado = mascaraTelefone(tel);
    
26.07.2017 / 02:48