Buscar

Máscara para o edit

Código

Máscara para formatar o Edit, esta função aceita apenas números, ideal para CPF, CNJP, data, telefone, cep
Ex: Mascara(Edit1.Text, '99.999.999/9999-99');
---------------------------------------------------


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    function Mascara(edt: String;str:String):string;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Edit1.Text := Mascara(Edit1.Text,'99/99/9999');
  Edit1.SelStart := Length(Edit1.Text);
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if not (Key in ['0'..'9',#8]) then Abort;
end;

function TForm1.Mascara(edt: String;str:String):string;
var
  i : integer;
begin
  for i := 1 to Length(edt) do
  begin
     if (str[i] = '9') and not (edt[i] in ['0'..'9']) and (Length(edt)=Length(str)+1) then
        delete(edt,i,1);
     if (str[i] <> '9') and (edt[i] in ['0'..'9']) then
        insert(str[i],edt, i);
  end;
  result := edt;
end;

Publicidade

Vote na dica




Quantidade de votos: 1 voto
Aceitação: 20%


Detalhes da dica

Categoria: Object pascal
Adicionada dia: 27/09/06
Por: Paulo Tomio Hoshino
Visualizada: 37234 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009