Mask in html field does not work

Asked

Viewed 830 times

1

Good afternoon,

I have a page in my application to register customers , nothing out of the ordinary.

On this page , I have some fields ( cif ,rg , phone,phone_2 ,etc) that when typing are formatted with javascript with the following function:

*Turning the telephone : 1199999999 into (11)9999-9999

// Mascaras Javascript

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function id( el ){
    return document.getElementById( el );
}

// aqui começa as mascaras 

function mtel(v){ //MASCARA PARA TELEFONE

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

window.onload = function atribuiMascaras(){ // FUNCAO QUE É ACIONADO AO CARREGAR A PAGINA ( WINDOW.ONLOAD )

    id('txtCel').onkeyup = function(){ //ATRIBUI O CAMPO COM ID txtCel A MASCARA DE TELEFONE
        mascara( this, mtel );
    }

It turns out, I can access this page and already have partially the data of that contact that is becoming a customer.

If I already have this partial data , when accessing the page , I take the contact ID from the Url and make a call to a controller to get the data from this client.

$(document).ready(function selectDadosContato(){        

var id_contato = <?=$dados[0];?>;
var tipo = '<?=$dados[1];?>';

jQuery.ajax
    ({  

        url: "/admin2/controllers/contatos/Contatos.php?id="+id_contato+"&tipo="+tipo, //URL de destino
        dataType: "json", //Tipo de Retorno
        success:
        function(data) {    

                var d = data.data_nascimento;
                var d = d.split('-');
                var data_nasc = d[2] + '/' + d[1] + '/' + d[0];

                document.getElementById('txtNome').value = data.nome;
                document.getElementById('txtCel').value = data.telefone;                    

             } //function(data);    
        });
});

Here is my problem , when I request and take the contact data and assign the value received in the html fields, I would need the masks of the fields to be activated, but the fields are filled in the way they come from the database.

From now on, grateful

2 answers

1

Just assign the masks on the return of your ajax

mascara( document.getElementById('txtCel'), mtel);

jQuery.ajax
    ({  

        url: "/admin2/controllers/contatos/Contatos.php?id="+id_contato+"&tipo="+tipo, //URL de destino
        dataType: "json", //Tipo de Retorno
        success:
        function(data) {    

                var d = data.data_nascimento;
                var d = d.split('-');
                var data_nasc = d[2] + '/' + d[1] + '/' + d[0];

                document.getElementById('txtNome').value = data.nome;
                document.getElementById('txtCel').value = data.telefone;      

                //Chame a sua função de mascara         
                mascara( document.getElementById('txtCel'), mtel);

             } //function(data);    
        });
});
  • 1

    You can not accept an answer in 6 minutes.... Gave right friend , had broken his head the afternoon trying to solve this hehe . Thanks !

  • @jefeson Assis , a doubt , I tried to call the mask to another field : mascara( Document.getElementById('txtFixo'), tel); . Thus staying with the call to 2 different fields. But so , only worked the mask of the field 'txtFixo' and the field 'txtCel' did not work . Why ?

  • Should not call the variable mtel - mascara( document.getElementById('txtCel'), mtel);? if it doesn’t work, check your browser console to see if an error appears

  • Good morning @Jeferson, my typo. Still yes , I tried putting all masks ( of Cpf,rg , phone, email , etc ) and only the last call mask works. On the console, it shows no error or warning

0


  • Good morning Alexandre , really did not know the plugin. At the doc has a lot of material on and seems to be very complete. Gave straight the masks. Thanks !

Browser other questions tagged

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