Ir ao topo

Tecnobyte

Logomarca da Tecnobyte
Contato por WhatsApp

WhatsApp

(69) 3421-6756

Contato por Telefone

(69) 3421-6756

(69) 3421-6757

Enviar mensagem

Enviar

mensagem

Contato por Facebook

Facebook

Vídeos

Vídeos

Atendimento de segunda a sexta, das 08h00 às 19h00 (horário de Brasília).

Banner

Delphi - Outros

Como formatar um CEP?

{ Esta função forma CEP como: 99.999-999 }
function tbFormataCEP(const CEP: string): string;
var
  I: integer;
begin
  Result := '';
  for I := 1 to Length(CEP) do
    if CEP[I] in ['0'..'9'] then
      Result := Result + CEP[I];
  if Length(Result) <> 8 then
    raise Exception.Create('CEP inválido.')
  else
    Result :=
      Copy(Result, 1, 2) + '.' +
      Copy(Result, 3, 3) + '-' +
      Copy(Result, 6, 3);
end;

=== Para testar ===

- Coloque um Edit e um Button no form;
- No evento OnClick do Button coloque a instrução abaixo:

  Edit1.Text := tbFormataCEP(Edit1.Text);

Observações

Para formatar outros códigos como CPF, CGC, etc., pode-se usar a mesma idéia.

O conteúdo desta página pode ajudar alguém? Compartilhe!