SlideShare a Scribd company logo
1 of 6
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()
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
}
// 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
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));
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];
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;

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Funções para validação

  • 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. 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. // 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. 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. 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. 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;