Veja aqui como formatar CPF em tempo de execucao do sistema
16/09/2005
0
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
Post mais votado
31/10/2012
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);
Claudia Nogueira
Mais Posts
17/09/2005
Adriano_servitec
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+
17/09/2005
Dopi
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
18/09/2005
Charlesgarrotti
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
31/10/2012
Alessandro Ribeiro
01/11/2012
Alessandro Ribeiro
Clique aqui para fazer login e interagir na Comunidade :)