Validação CNPF
08/09/2008
0
Criei uma rotina para poder validar o CNPJ, mais nao e um campo obrigatorio. O meu grande problema foi quando deparei com o seguinte problema se o usuario digitar 2 caracter iria permitir salvar e me gerari um transtorno acabei fazendo o procedimento abaixo. Alguem poderia me ajudar a melhorar este codigo ate para poder ser postado. Pois no futuro pode ser a duvida de alguém:
[/i][/b]
[b][i]procedure Ttransp.DBEdit3Exit(Sender: TObject); Var n: array[1..14]of integer; dgt1, dgt2: integer; soma1, soma2:integer; begin inherited; If (copy(dbEdit3.Text,1,1)<>´ ´) and (copy(dbEdit3.Text,2,1)<>´ ´) and (copy(dbEdit3.Text,4,1)<>´ ´) and (copy(dbEdit3.Text,5,1)<>´ ´) and (copy(dbEdit3.Text,6,1)<>´ ´) and (copy(dbEdit3.Text,8,1)<>´ ´) and (copy(dbEdit3.Text,9,1)<>´ ´) and (copy(dbEdit3.Text,10,1)<>´ ´) and (copy(dbEdit3.Text,12,1)<>´ ´) and (copy(dbEdit3.Text,13,1)<>´ ´) and (copy(dbEdit3.Text,14,1)<>´ ´) and (copy(dbEdit3.Text,15,1)<>´ ´) and (copy(dbEdit3.Text,17,1)<>´ ´) and (copy(dbEdit3.Text,18,1)<>´ ´) Then Begin { // PREPARANDO OS NUMEROS PARA O CALCULO \\} n[1]:= strToInt(copy(dbEdit3.Text,1,1)); n[2]:= strToInt(copy(dbEdit3.Text,2,1)); n[3]:= strToInt(copy(dbEdit3.Text,4,1)); n[4]:= strToInt(copy(dbEdit3.Text,5,1)); n[5]:= strToInt(copy(dbEdit3.Text,6,1)); n[6]:= strToInt(copy(dbEdit3.Text,8,1)); n[7]:= strToInt(copy(dbEdit3.Text,9,1)); n[8]:= strToInt(copy(dbEdit3.Text,10,1)); n[9]:= strToInt(copy(dbEdit3.Text,12,1)); n[10]:= strToInt(copy(dbEdit3.Text,13,1)); n[11]:= strToInt(copy(dbEdit3.Text,14,1)); n[12]:= strToInt(copy(dbEdit3.Text,15,1)); n[13]:= strToInt(copy(dbEdit3.Text,17,1)); n[14]:= strToInt(copy(dbEdit3.Text,18,1)); { // CALCULO DO PRIMEIRO DÍGITO VERIFICADOR \\ } Soma1:= (n[1]*5) + (n[2]*4) + (n[3]*3) + (n[4]*2) + (n[5]*9) + (n[6]*8) + (n[7]*7) + (n[8]*6) + (n[9]*5) + (n[10]*4) + (n[11]*3) + (n[12]*2); dgt1:= 11 - (Soma1 mod 11); If (dgt1 = 10) or (dgt1 = 11) Then dgt1:= 0; { // CALCULO DO SEGUNDO DÍGITO VERIFICADOR \\ } Soma2:= (n[1]*6) + (n[2]*5) + (n[3]*4) + (n[4]*3) + (n[5]*2) + (n[6]*9) + (n[7]*8) + (n[8]*7) + (n[9]*6) + (n[10]*5) + (n[11]*4) + (n[12]*3) + (dgt1 * 2); dgt2:= 11 - (Soma2 mod 11); If (dgt2 = 10) or (dgt2 = 11) Then dgt2:= 0; { // CONFERINDO OS DIGITOS VERIFICADORES \\ } If (dgt1 <> n[13]) or (dgt2 <> n[14]) Then begin mensagem:= ´Numero do CNPJ Inválido. ´; Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; abort; end; End Else Begin { // VERIFICANDO SE FORAM DIGITADOS OS 14 DÍGITOS \\ } mensagem:= ´CNPJ incompleto.´ + #13 +´Digite novamente´+ 13 + ´os 14 dígitos. ´; if not (copy(dbEdit3.Text,1,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,2,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,4,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,5,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,6,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,8,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,9,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,10,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,12,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,13,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,14,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,15,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,17,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end Else If not (copy(dbEdit3.Text,18,1)=´ ´) then begin Application.MessageBox(Pchar(mensagem), ´Erro no CNPJ...´, MB_OK + MB_IconError); dbEdit3.SetFocus; dbEdit3.Clear; Abort; end; End; end;
Serpentedodeserto
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)