Formatar CPF em um DataGrid
Preciso formatar o CPF no datagrid já tentei :
{0:G#..-}
mas não obtive sucesso.
{0:G#..-}
mas não obtive sucesso.
Sandroperes
Curtidas 0
Respostas
Xrafaelx
11/12/2007
Preciso formatar o CPF no datagrid já tentei :
{0:G#..-}
mas não obtive sucesso.
usa um javascript para formatar
GOSTEI 0
Pauloroger
11/12/2007
Use AjaxToolKit, vc não usa RAD Studio ???
segue link [url]http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx[/url]
segue link [url]http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx[/url]
GOSTEI 0
Sandroperes
11/12/2007
estou usando delphi 2006
Usei esse javascript para formatar o CPF
<script type=´text/javascript´>
function formatar_mascara(src, mascara) {
var campo = src.value.length;
var saida = mascara.substring(0,1);
var texto = mascara.substring(campo);
if(texto.substring(0,1) != saida) {
src.value += texto.substring(0,1);
}
}
</script>
E na coluna coloquei
<ASP:HyperLinkColumn datanavigateurlfield=´CAD_CODIGO´ datanavigateurlformatstring=´pInclusaoGuia.aspx?ID=´ datatextfield=´CAD_CPF´ headertext=´CPF´ datatextformatstring=´Javascript:formatar_mascara(,´#..-´}´></ASP:HyperLinkColumn>
Mas infelizmente não surgiu efeito.. O que posso estar errando?
Usei esse javascript para formatar o CPF
<script type=´text/javascript´>
function formatar_mascara(src, mascara) {
var campo = src.value.length;
var saida = mascara.substring(0,1);
var texto = mascara.substring(campo);
if(texto.substring(0,1) != saida) {
src.value += texto.substring(0,1);
}
}
</script>
E na coluna coloquei
<ASP:HyperLinkColumn datanavigateurlfield=´CAD_CODIGO´ datanavigateurlformatstring=´pInclusaoGuia.aspx?ID=´ datatextfield=´CAD_CPF´ headertext=´CPF´ datatextformatstring=´Javascript:formatar_mascara(,´#..-´}´></ASP:HyperLinkColumn>
Mas infelizmente não surgiu efeito.. O que posso estar errando?
GOSTEI 0
Xrafaelx
11/12/2007
FUNCIONA NO MOZILA E NO IE
<script language=´JavaScript´>
//-----> somente números:
function v_NR(tecla){
if(typeof(tecla) == ´undefined´)
var tecla = window.event;
var codigo = (tecla.which ? tecla.which : tecla.keyCode ? tecla.keyCode : tecla.charCode);
// permite números, 8=backspace, 46=del e 9=tab
if ( (codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || codigo == 8 || codigo == 46 || codigo == 9 )
{ return true; }
else
{ alert(´Apenas números são permitidos !´); return false; }
}
//-----> máscara cpf:
function m_CPF(campo,tammax) {
var vr = campo.value;
vr = vr.replace( ´-´, ´´ );
vr = vr.replace( ´.´, ´´ );
vr = vr.replace( ´.´, ´´ );
var tam = vr.length;
if (tam < tammax) { tam = vr.length + 1; }
tam = tam - 1;
if ( (tam > 2) && (tam <= 11) ) {
vr = vr.substr( 0, tam - 1 ) + ´-´ + vr.substr( tam - 1, tam ); }
if ( (tam == 10) ) {
vr = vr.substr( 0, tam - 7 ) + ´.´ + vr.substr( tam - 7, 3 ) + ´.´ + vr.substr( tam - 4, tam ); }
campo.value = vr;
}
</script>
<asp:TextBox id=´TextBox1´ runat=´server´ onKeyPress=´m_CPF(this,11); return v_NR(event)´ onKeyDown=´return v_NR(event)´ onChange=´m_CPF(this,11)´ maxlength=´14´></asp:TextBox>
<script language=´JavaScript´>
//-----> somente números:
function v_NR(tecla){
if(typeof(tecla) == ´undefined´)
var tecla = window.event;
var codigo = (tecla.which ? tecla.which : tecla.keyCode ? tecla.keyCode : tecla.charCode);
// permite números, 8=backspace, 46=del e 9=tab
if ( (codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || codigo == 8 || codigo == 46 || codigo == 9 )
{ return true; }
else
{ alert(´Apenas números são permitidos !´); return false; }
}
//-----> máscara cpf:
function m_CPF(campo,tammax) {
var vr = campo.value;
vr = vr.replace( ´-´, ´´ );
vr = vr.replace( ´.´, ´´ );
vr = vr.replace( ´.´, ´´ );
var tam = vr.length;
if (tam < tammax) { tam = vr.length + 1; }
tam = tam - 1;
if ( (tam > 2) && (tam <= 11) ) {
vr = vr.substr( 0, tam - 1 ) + ´-´ + vr.substr( tam - 1, tam ); }
if ( (tam == 10) ) {
vr = vr.substr( 0, tam - 7 ) + ´.´ + vr.substr( tam - 7, 3 ) + ´.´ + vr.substr( tam - 4, tam ); }
campo.value = vr;
}
</script>
<asp:TextBox id=´TextBox1´ runat=´server´ onKeyPress=´m_CPF(this,11); return v_NR(event)´ onKeyDown=´return v_NR(event)´ onChange=´m_CPF(this,11)´ maxlength=´14´></asp:TextBox>
GOSTEI 0
Sandroperes
11/12/2007
Valeu, vou tentar a sua sugestão.
GOSTEI 0
Sandroperes
11/12/2007
Olá, mas como eu faço a chamada em um HyperLinkColumn eu coloquei:
javascript:m_CPF(,11), ele esta me mostrando
javascript:m_CPF(12345678901,11)
desde já agradeço pelo auxilio.
javascript:m_CPF(,11), ele esta me mostrando
javascript:m_CPF(12345678901,11)
desde já agradeço pelo auxilio.
GOSTEI 0
Xrafaelx
11/12/2007
public
{ Public Declarations }
function MascaraCPF(cpf:&object) :String;
implementation
{....}
function TWebForm1.MascaraCPF(cpf:&object) :String;
var vCpf:Double;
begin
vCpf := Convert.ToDouble(cpf);
Result := System.String.Format(´{0:000\.000\.000-00}´,[vCPF]);
end;
no datagrid
<asp:TemplateColumn>
<ItemTemplate><asp:HyperLink id=´HyperLink1´ runat=´server´ text=´<¬# this.MascaraCpf(DataBinder.Eval(Container,´DataItem.CPF´))¬>´ /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
pronto :roll:
{ Public Declarations }
function MascaraCPF(cpf:&object) :String;
implementation
{....}
function TWebForm1.MascaraCPF(cpf:&object) :String;
var vCpf:Double;
begin
vCpf := Convert.ToDouble(cpf);
Result := System.String.Format(´{0:000\.000\.000-00}´,[vCPF]);
end;
no datagrid
<asp:TemplateColumn>
<ItemTemplate><asp:HyperLink id=´HyperLink1´ runat=´server´ text=´<¬# this.MascaraCpf(DataBinder.Eval(Container,´DataItem.CPF´))¬>´ /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
pronto :roll:
GOSTEI 0
Sandroperes
11/12/2007
Obrigado, vou tentar a sua sugestão.
GOSTEI 0
Sandroperes
11/12/2007
Show de Bola!!!!
Funcionou.
Funcionou.
GOSTEI 0
Sandroperes
11/12/2007
Hã estava ne esquecendo eu tive que alterar a função
function TWebForm1.MascaraCPF(cpf:&object) :String;
var vCpf:Double;
begin
if cpf.ToString = ´´ then
vCpf := Convert.ToDouble(&Object(0))
else
vCpf := Convert.ToDouble(cpf);
Result := System.String.Format(´{0:000\.000\.000-00}´,[vCPF]);
end;
function TWebForm1.MascaraCPF(cpf:&object) :String;
var vCpf:Double;
begin
if cpf.ToString = ´´ then
vCpf := Convert.ToDouble(&Object(0))
else
vCpf := Convert.ToDouble(cpf);
Result := System.String.Format(´{0:000\.000\.000-00}´,[vCPF]);
end;
GOSTEI 0