Veja aqui como formatar CPF em tempo de execucao do sistema
Sempre necessitando de algo que me pudesse colocar mascaras, já cansado de ficar usando Edimask nos Field, e Maskededit´s, resolvi criar uma funçãozinha que atendesse tanto em CPF, CNPJ´s como em telefones.
A função é a seguinte:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
s:string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject);
begin
edit1.MaxLength := 14;
if s = ´s´ then
begin
if length(edit1.Text) = 3 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 7 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 11 then
begin
edit1.Text := edit1.Text + ´-´;
edit1.SelStart := length(edit1.Text);
end;
end
else
begin
s :=´s´;
end;
end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = VK_BACK then
begin
s:=´n´;
end;
end;
end.
é isso dai pessoal, funciona legal !!!!
Charles Garrotti
Analista de Sistemas
A função é a seguinte:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
s:string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject);
begin
edit1.MaxLength := 14;
if s = ´s´ then
begin
if length(edit1.Text) = 3 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 7 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 11 then
begin
edit1.Text := edit1.Text + ´-´;
edit1.SelStart := length(edit1.Text);
end;
end
else
begin
s :=´s´;
end;
end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = VK_BACK then
begin
s:=´n´;
end;
end;
end.
é isso dai pessoal, funciona legal !!!!
Charles Garrotti
Analista de Sistemas
Charlesgarrotti
Curtidas 0
Melhor post
Claudia Nogueira
31/10/2012
Vê se com essa fica melhor:
E no onChange do Edit você chama a função, passando o parâmetro se é CPF ou CNPJ:
function TForm1.FormataCpfCnpj(aTextoP, aTipoP: String): String; begin aTextoP := SoNumero(aTextoP); if aTipoP = CPF then begin if Length(aTextoP) >= 4 then Insert(.,aTextoP,4); if Length(aTextoP) >= 8 then Insert(.,aTextoP,8); if Length(aTextoP) >= 12 then Insert(-,aTextoP,12); aTextoP := Copy(aTextoP,1,14); end else begin if Length(aTextoP) >= 3 then Insert(.,aTextoP,3); if Length(aTextoP) >= 7 then Insert(.,aTextoP,7); if Length(aTextoP) >= 11 then Insert(/,aTextoP,11); if Length(aTextoP) >= 16 then Insert(-,aTextoP,16); aTextoP := Copy(aTextoP,1,18); end; Result := aTextoP; end; function TForm1.SoNumero(aTextoP: String): String; Var i : Integer; s : String; begin s := ; for i := 1 To Length(aTextoP) Do begin if (aTextoP[i] in [0..9]) then s := s + Copy(aTextoP, i, 1); end; Result := s; end;
E no onChange do Edit você chama a função, passando o parâmetro se é CPF ou CNPJ:
Edit1.Text := FormataCpfCnpj(Edit1.Text,CPF); Edit1.SelStart := Length(Edit1.Text);
Oi Pessoal, utilizei as duas funções acima para formatar em tempo de digitação. Dá um efeito muito legal, ficou 10, só tô com problema no momento em que o usuário tenta apagar, por exemplo, se eu apagar um ponto e em seguida digitar um valor, ele perde totalmente a formatação ai fica estranho, alguém ai tem alguma idéia de como podemos resolver esse problema ou alguma ação que eu possa tomar para evitar isso. Abração a todos ai
GOSTEI 1
Mais Respostas
Adriano_servitec
16/09/2005
E isso ai amigo Charles Garrotti, fiz seu exemplo e funcionou direitinho e estou colocando aqui como se faz mascara para CNPJ com sua funçao para quem nao entendeu ainda
procedure TForm1.Edit1Change(Sender: TObject);
begin
edit1.MaxLength := 18;
if s = ´s´ then
begin
if length(edit1.Text) = 2 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 6 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 10 then
begin
edit1.Text := edit1.Text + ´/´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 15 then
begin
edit1.Text := edit1.Text + ´-´;
edit1.SelStart := length(edit1.Text);
end;
Valeu pela dica amigo
T+
procedure TForm1.Edit1Change(Sender: TObject);
begin
edit1.MaxLength := 18;
if s = ´s´ then
begin
if length(edit1.Text) = 2 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 6 then
begin
edit1.Text := edit1.Text + ´.´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 10 then
begin
edit1.Text := edit1.Text + ´/´;
edit1.SelStart := length(edit1.Text);
end;
if length(edit1.Text) = 15 then
begin
edit1.Text := edit1.Text + ´-´;
edit1.SelStart := length(edit1.Text);
end;
Valeu pela dica amigo
T+
GOSTEI 0
Gigatel
16/09/2005
Esta função valida CNPJ e CPF num mesmo edit ?
GOSTEI 0
Dopi
16/09/2005
Acho que pode ser do interesse...
Formatando e validando CNPJ /CPF (ou outros documentos) usando o componente [b:7f50b50c52]ACBrValidador[/b:7f50b50c52]
http://www.forumweb.com.br/foruns/index.php?showtopic=33861
Formatando e validando CNPJ /CPF (ou outros documentos) usando o componente [b:7f50b50c52]ACBrValidador[/b:7f50b50c52]
http://www.forumweb.com.br/foruns/index.php?showtopic=33861
GOSTEI 0
Charlesgarrotti
16/09/2005
Esta função valida CNPJ e CPF num mesmo edit ?
Essa Função acima somente ´FORMATA´ o edit, para fazer a validação teria que implementar a funcao assim:
function ValidaCPFCNPJ(CPFCNPJ,TIPO:String): Boolean;
begin
if tipo = ´CPF´ then
begin
aqui voce coloca toda a rotina para formatar o CPF e fazer a validação do numero
end;
if tipo = ´CNPJ´ then
begin
aqui voce coloca toda a rotina para formatar o CNPJ e fazer a validação do numero
end;
end;
usando essa funcao voce tem um FORMATADOR e um VALIDAR em tempo Real !!!
Charles Garrotti
GOSTEI 0
Alessandro Ribeiro
16/09/2005
Oi Pessoal, utilizei as duas funções acima para formatar em tempo de digitação. Dá um efeito muito legal, ficou 10, só tô com problema no momento em que o usuário tenta apagar, por exemplo, se eu apagar um ponto e em seguida digitar um valor, ele perde totalmente a formatação ai fica estranho, alguém ai tem alguma idéia de como podemos resolver esse problema ou alguma ação que eu possa tomar para evitar isso. Abração a todos ai
GOSTEI 0
Alessandro Ribeiro
16/09/2005
Claudia, acabei de testar, era realmente esse efeito que eu estava precisando, muitíssimo obrigado. Diga-se ficou muito maneiro!!!!!!!!!!!!!!!!
GOSTEI 0
Gxf
16/09/2005
isso meu ajudou em 2021! rsrs
GOSTEI 0