Função que verifica se o numero se trata de um cpf ou um cnpj já validando o seu conteudo
function LimpaCNPJ(CNPJ : String) : string; var Retorno : String; Cont : Integer; begin for cont := 1 to length(CNPJ) do begin if (CNPJ[cont] <> '.') and (CNPJ[cont] <> '/') and (CNPJ[cont] <> '-') and (CNPJ[cont] <> ',') then begin try Retorno := Retorno + inttostr(strtoint(CNPJ[cont])); except end; end; end; Result := Retorno; end; function ValidaCnpjCpf(Numero : string; var Fisica : Boolean) : Boolean; var i,j,k : Integer; Soma : Integer; Digito : Integer; Retorno : Boolean; NumOriginal : string; begin Retorno := True; NumOriginal := LimpaCNPJ(Numero); Numero := LimpaCNPJ(Numero); Numero := Copy(Numero,0,length(Numero) -2); case length(Numero) of 9 : Fisica := True; 12 : Fisica := False; else Retorno := False end; {Verificando se todos os caracteres são iguais} Retorno := false; for i := 1 to Length(Numero) do begin if ((i+1) <= Length(Numero)) then begin if (Numero[i+1] <> numero[i]) then begin Retorno := true; Break; end; end; end; if Retorno then begin for J := 1 to 2 do begin k := 2; Soma := 0; for i := Length(Numero) downto 1 do begin Soma := Soma + (ord(Numero[i]) - Ord('0'))*k; Inc(k); if ((k > 9) and (not Fisica)) then k := 2; end; Digito := 11 - Soma mod 11; if Digito >= 10 then Digito := 0; Numero := Numero + IntToStr(Digito); end; Retorno := NumOriginal = Numero; end; result := Retorno; end;