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
You can not accept an answer in 6 minutes.... Gave right friend , had broken his head the afternoon trying to solve this hehe . Thanks !
– Henrique Felix
@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 ?
– Henrique Felix
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– Jeferson Assis
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
– Henrique Felix