Change phone mask

Asked

Viewed 691 times

3

Hello!

I’m trying to alter a mask from a field of my form. Below in the field highlighted in blue, is as it is currently, and in the second field is as wish it remains (without the space between the ) and the 1). I wish to change this because by allowing the data to be sent in the first way, I have some problems when returning results from the database.

Máscara

Below the Javascript code that is making the mask.

I tried to remove the space after ) on the line texto = [texto.slice(0, 3), ") " but I had problems with the operation of the field.

$("#txtTelefone2").bind('input propertychange', function () {

            var texto = $(this).val();

            texto = texto.replace(/[^\d]/g, '');

            if (texto.length > 0) {
                texto = "(" + texto;

                if (texto.length > 3) {
                    texto = [texto.slice(0, 3), ") ", texto.slice(3)].join('');
                }
                if (texto.length > 12) {
                    if (texto.length > 13)
                        texto = [texto.slice(0, 10), "-", texto.slice(10)].join('');
                    else
                        texto = [texto.slice(0, 9), "-", texto.slice(9)].join('');
                }
                if (texto.length > 15)
                    texto = texto.substr(0, 15);
            }
            $(this).val(texto);
        })

How can I fix this? Thank you!

2 answers

7

Here is a suggestion:

$("#txtTelefone2").bind('input propertychange', function (e) {

    var numeros = this.value.replace(/[\s()-]*/g, '').split('');
    if (numeros.length > 12) {
        this.value = this.value.slice(0,-1);
        return false;
    }
    var posicaoHifen = numeros.length == 12 ? 8 : 7;
    var formatado = '';
    for (var i = 0; i < numeros.length; i++) {
        if (i == 0) formatado += '(';
        if (i == 2) formatado += ')';
        if (i == posicaoHifen) formatado += '-';
        formatado += numeros[i];
    }
    this.value = formatado;
});

Demo: http://jsfiddle.net/HP2Wx/1/

Explanation:

    this.value.replace(/[\s()-]*/g, '').split('');

Create an array only with numbers. Clearing spaces and parentheses.

    if (numeros.length > 11) {
        this.value = this.value.slice(0,-1);
        return false;
    }

In case the maximum length has been reached, remove the last value and stop the function

    var posicaoHifen = numeros.length == 12 ? 8 : 7;

Check the total length in number and change the hyphen position

    var formatado = '';
    for (var i = 0; i < numeros.length; i++) {
        if (i == 0) formatado += '(';
        if (i == 2) formatado += ')';
        if (i == 7) formatado += '-';
        formatado += numeros[i];
    }

Add the desired characters to the input, depending on the position of the number during the iteration of the number array

  • 1

    Sergio, thank you for the answer, actually works for the proposed problem, but the hyphen shift is not working as it was originally. + 1 for the good explanation.

  • @Victoralencarsantos, Hmmm hifen displacement was not requested in the question. Can you explain what different cases you want for the hyphenation position and for what length? So I can at least leave the right answer.

  • Hi @Sergio. Originally, the hyphen was positioned like this: (xx) xxxx-xxxx if they were 8-digit numbers, and so (xx) xxxxx-xxxx if they were 9-digit numbers.

  • @Victoralencarsantos, thanks. I improved the answer to if anyone else needs.

  • Hello @Sergio! The form of displacement is correct, but using your Fiddle in this example are being inserted 6 numbers before the hyphen (11)111111-1111. See here as it was originally, if you want to make any changes. Thank you!

  • @Victoralencarsantos, okay, is that how it should work? : http://jsfiddle.net/HP2Wx/2/

  • 1

    Perfect, @Sergio!

Show 2 more comments

4


Would look like this:

$("#txtTelefone2").bind('input propertychange', function () {

        var texto = $(this).val();

        texto = texto.replace(/[^\d]/g, '');

        if (texto.length > 0) {
            texto = "(" + texto;

            if (texto.length > 3) {
                texto = [texto.slice(0, 3), ")", texto.slice(3)].join('');
            }
            if (texto.length == 12) {
                texto = [texto.slice(0, 8), "-", texto.slice(8)].join('');                    
            }
            else if (texto.length > 12)
                    texto = [texto.slice(0, 9), "-", texto.slice(9)].join('');

            if (texto.length > 14)
                texto = texto.substr(0, 14);
        }
        $(this).val(texto);
    })

Demo: http://fiddle.jshell.net/5z29v/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.