Funçãoparavalidar email
Function ValidaEMail(const EMailIn: PChar):Boolean;
const
CaraEsp: array[1..40] of string[1] =
( '...
Função para validar cpf
Function testacpf(cpf:string):boolean;
var i:integer;
Want:char;
Wvalid:boolean;
Wdigit1,Wdigit2:i...
// confere o 2° digito verificador
if IntToStr(Wdigit2) <>cpf[11] then
begin
result:=false;
exit;
end;
//se chegar até aqu...
444444444',
'55555555555','66666666666','77777777777','88888888888','99999999999']) of
0..9: ret:=False;
end;
end
else
beg...
end;
dg1:=11-(total mod 11);
if dg1 >9then
dg1:=0;
//2° digito
total:=0;
for x:=1 to 13do
begin
if x <6then
Inc(total,StrT...
if Length(Result) <> 8 then
raise Exception.Create('CEP inválido.')
else
Result := Copy(Result, 1, 2) + '.' + Copy(Result,...
Upcoming SlideShare
Loading in...5
×

Funções para validação

221

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
221
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Funções para validação"

  1. 1. Funçãoparavalidar email Function ValidaEMail(const EMailIn: PChar):Boolean; const CaraEsp: array[1..40] of string[1] = ( '!','#','$','%','¨','&','*', '(',')','+','=','§','¬','¢','¹','²', '³','£','´','`','ç','Ç',',',';',':', '<','>','~','^','?','/','','|','[',']','{','}', 'º','ª','°'); var i,cont : integer; EMail : ShortString; begin EMail := EMailIn; Result := True; cont := 0; if EMail<> '' then if (Pos('@', EMail)<>0) and (Pos('.', EMail)<>0) then // existe @ . begin if (Pos('@', EMail)=1) or (Pos('@', EMail)= Length(EMail)) or (Pos('.', EMail)=1) or (Pos('.', EMail)= Length(EMail)) or (Pos(' ', EMail)<>0) then Result := False else // @ seguido de . e vice-versa if (abs(Pos('@', EMail) - Pos('.', EMail)) = 1) then Result := False else begin for i := 1 to 40 do // se existeCaracter Especial if Pos(CaraEsp[i], EMail)<>0 then Result := False; for i := 1 to length(EMail) do begin // se existeapenas 1 @ if EMail[i] = '@' then cont := cont + 1; // . seguidos de . if (EMail[i] = '.') and (EMail[i+1] = '.') then Result := false; end; // . no f, 2ou+ @, . noi, - no i, _ no i if (cont>=2) or ( EMail[length(EMail)]= '.' ) or ( EMail[1]= '.' ) or ( EMail[1]= '_' ) or ( EMail[1]= '-' ) then Result := false; // @ seguido de COM e vice-versa if (abs(Pos('@', EMail) - Pos('com', EMail)) = 1) then Result := False; // @ seguido de - e vice-versa if (abs(Pos('@', EMail) - Pos('-', EMail)) = 1) then Result := False; // @ seguido de _ e vice-versa if (abs(Pos('@', EMail) - Pos('_', EMail)) = 1) then Result := False; end; end else Result := False; end; If pos()
  2. 2. Função para validar cpf Function testacpf(cpf:string):boolean; var i:integer; Want:char; Wvalid:boolean; Wdigit1,Wdigit2:integer; begin Wdigit1:=0; Wdigit2:=0; Want:=cpf[1];//variavel para testar se o cpf é repetido como 111.111.111-11 Delete(cpf,ansipos('.',cpf),1); //retira as mascaras se houver Delete(cpf,ansipos('.',cpf),1); Delete(cpf,ansipos('-',cpf),1); //testar se o cpf é repetido como 111.111.111-11 for i:=1 to length(cpf) do begin if cpf[i] <>Want then begin Wvalid:=true; // se o cpf possui um digito diferente ele passou no primeiro teste break end; end; // se o cpf é composto por numeros repetido retorna falso if not Wvalid then begin result:=false; exit; end; //executa o calculo para o primeiro verificador for i:=1 to 9 do begin wdigit1:=Wdigit1+(strtoint(cpf[10-i])*(I+1)); end; Wdigit1:= ((11 - (Wdigit1 mod 11))mod 11) mod 10; {formula do primeiro verificador soma=1°*2+2°*3+3°*4.. até 9°*10 digito1 = 11 - soma mod 11 se digito > 10 digito1 =0 } //verifica se o 1° digito confere if IntToStr(Wdigit1) <>cpf[10] then begin result:=false; exit; end; for i:=1 to 10 do begin wdigit2:=Wdigit2+(strtoint(cpf[11-i])*(I+1)); end; Wdigit2:= ((11 - (Wdigit2 mod 11))mod 11) mod 10; {formula do segundo verificador soma=1°*2+2°*3+3°*4.. até 10°*11 digito1 = 11 - soma mod 11 se digito > 10 digito1 =0 }
  3. 3. // confere o 2° digito verificador if IntToStr(Wdigit2) <>cpf[11] then begin result:=false; exit; end; //se chegar até aqui o cpf é valido result:=true; end; Funçãoparavalidarcpf 2 functionTForm1.ValidaCPF(numCPF:string):boolean; var cpf:string; x, total, dg1, dg2:Integer; ret:boolean; begin ret:=True; for x:=1 to Length(numCPF)do ifnot(numCPF[x]in['0'..'9','-','.',' '])then ret:=False; if ret then begin ret:=True; cpf:=''; for x:=1 to Length(numCPF)do ifnumCPF[x]in['0'..'9']then cpf:=cpf+numCPF[x]; ifLength(cpf)<>11then ret:=False; if ret then begin //1° dígito total:=0; for x:=1 to 9do total:=total +(StrToInt(cpf[x])* x); dg1:=total mod 11; if dg1 =10then dg1:=0; //2° dígito total:=0; for x:=1 to 8do total:=total +(StrToInt(cpf[x +1])*(x)); total:=total +(dg1 *9); dg2:=total mod 11; if dg2 =10then dg2:=0; //Validação final if dg1 =StrToInt(cpf[10])then if dg2 =StrToInt(cpf[11])then ret:=True; //Inválidos caseAnsiIndexStr(cpf,['00000000000','11111111111','22222222222','33333333333','44
  4. 4. 444444444', '55555555555','66666666666','77777777777','88888888888','99999999999']) of 0..9: ret:=False; end; end else begin //Se nãoinformadodeixapassar ifcpf=''then ret:=True; end; end; ValidaCPF:=ret; end; Funçãoparavalidar CNPJ functionTForm1.ValidaCNPJ(numCNPJ:string):boolean; var cnpj:string; dg1, dg2: integer; x, total: integer; ret:boolean; begin ret:=False; cnpj:=''; //Analisaosformatos ifLength(numCNPJ)=18then if(Copy(numCNPJ,3,1)+Copy(numCNPJ,7,1)+Copy(numCNPJ,11,1)+Copy(numCNPJ,16,1)='../ -')then begin cnpj:=Copy(numCNPJ,1,2)+Copy(numCNPJ,4,3)+Copy(numCNPJ,8,3)+Copy(numCNPJ,12,4)+Co py(numCNPJ,17,2); ret:=True; end; ifLength(numCNPJ)=14then begin cnpj:=numCNPJ; ret:=True; end; //Verifica if ret then begin try //1° digito total:=0; for x:=1 to 12do begin if x <5then Inc(total,StrToInt(Copy(cnpj, x,1))*(6- x)) else Inc(total,StrToInt(Copy(cnpj, x,1))*(14- x));
  5. 5. end; dg1:=11-(total mod 11); if dg1 >9then dg1:=0; //2° digito total:=0; for x:=1 to 13do begin if x <6then Inc(total,StrToInt(Copy(cnpj, x,1))*(7- x)) else Inc(total,StrToInt(Copy(cnpj, x,1))*(15- x)); end; dg2:=11-(total mod 11); if dg2 >9then dg2:=0; //Validação final if(dg1 =StrToInt(Copy(cnpj,13,1)))and(dg2 =StrToInt(Copy(cnpj,14,1)))then ret:=True else ret:=False; except ret:=False; end; //Inválidos caseAnsiIndexStr(cnpj,['00000000000000','11111111111111','22222222222222','333333 33333333','44444444444444', '55555555555555','66666666666666','77777777777777','88888888888888','99999999999 999']) of 0..9: ret:=False; end; end; ValidaCNPJ:=ret; end; Funçãoparavalidar CEP Validando CEP Código: Function ValidarCEP(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];
  6. 6. 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;

×