Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido) Validar linha digitável cheque


Fireboard

Pergunta

alguém tem um layout ou sabe qual é a regra para validar a linha digitável de um cheque?

Para a linha magnética faço a validação normalmente, mas caso o usuário digite a linha digitável do cheque, se eu usar a mesma função não funciona.

Segue código que estou utilizando:

function TDm.validaCMC7(linha_dig: string): boolean;
var soma, total, num, i, i1, i2, i3, res: integer;
    c1, c2, c3, c4: string;
begin

    // Validar tamanho do campo
    if length(linha_dig) <> 34 then
    begin
        Result:= false;
        exit;
    end;


    //----------------------------------------- C1 ----------------------------------------

    c1:= AnsiMidStr(linha_dig,2,7);
    // Validar se contém apenas números
    for i:= length(c1) downto 1 do
    begin
        if (c1[i] <> '1') and
           (c1[i] <> '2') and
           (c1[i] <> '3') and
           (c1[i] <> '4') and
           (c1[i] <> '5') and
           (c1[i] <> '6') and
           (c1[i] <> '7') and
           (c1[i] <> '8') and
           (c1[i] <> '9') and
           (c1[i] <> '0') then
        begin
            result:= false;
            exit;
        end;

    end;
    i1:= strtoint(AnsiMidStr(linha_dig,22,1));


    //----------------------------------------- C2 ----------------------------------------


    {passar o campo conta corrente com uma string de 10 posicoes}
    c2:=  AnsiMidStr(linha_dig,11,10);
    // Validar se contém apenas números
    for i:= length(c2) downto 1 do
    begin
        if (c2[i] <> '1') and
           (c2[i] <> '2') and
           (c2[i] <> '3') and
           (c2[i] <> '4') and
           (c2[i] <> '5') and
           (c2[i] <> '6') and
           (c2[i] <> '7') and
           (c2[i] <> '8') and
           (c2[i] <> '9') and
           (c2[i] <> '0') then
        begin
            result:= false;
            exit;
        end;
    end;
    i2:= strtoint(AnsiMidStr(linha_dig,9,1));


    //----------------------------------------- C3 ----------------------------------------


    {passar o campo Numero do Cheque com uma string de 6 posicoes}
    c3:= AnsiMidStr(linha_dig,23,10);
    // Validar se contém apenas números
    for i:= length(c1) downto 1 do
    begin
        if (c3[i] <> '1') and
           (c3[i] <> '2') and
           (c3[i] <> '3') and
           (c3[i] <> '4') and
           (c3[i] <> '5') and
           (c3[i] <> '6') and
           (c3[i] <> '7') and
           (c3[i] <> '8') and
           (c3[i] <> '9') and
           (c3[i] <> '0') then
        begin
            result:= false;
            exit;
        end;
    end;
    i3:= strtoint(AnsiMidStr(linha_dig,33,1));




    //------------------------------- Cálculo C1 ------------------------------------
    soma:= 0;
    num:= 2;
    total:= 0;
    validaCMC7:= true;

    for i:= length(c1) downto 1 do
    begin
        soma:= 0;
        soma:= strtoint(c1[i]) * num;
        if soma >= 10 then
        begin
            c4:= inttostr(soma);
            soma:= strtoint(c4[1]) + strtoint(c4[2]);
        end;
        total:= total + soma;

        if num = 2 then
            num:= 1
        else
            num:= 2;
    end;
    res:= total mod 10;
    if res <> 0 then
        res:= 10 - res;

    if res <> i1 then
    begin
        validaCMC7:= false;
    end;

    //------------------------------- Cálculo C2 ------------------------------------
    soma:= 0;
    num:= 2;
    total:= 0;

    for i:= length(c2) downto 1 do
    begin
        soma:= 0;
        soma:= strtoint(c2[i]) * num;
        if soma >= 10 then
        begin
            c4:= inttostr(soma);
            soma:= strtoint(c4[1]) + strtoint(c4[2]);
        end;
        total:= total + soma;

        if num = 2 then
            num:= 1
        else
            num:= 2;
    end;
    res:= total mod 10;
    if res <> 0 then
        res:= 10 - res;

    if res <> i2 then
    begin
        validaCMC7:= false;
    end;

    //------------------------------- Cálculo C3 ------------------------------------
    soma:= 0;
    num:= 2;
    total:= 0;
    for i:= length(c3) downto 1 do
    begin
        soma:= 0;
        soma:= strtoint(c3[i]) * num;
        if soma >= 10 then
        begin
            c4:= inttostr(soma);
            soma:= strtoint(c4[1]) + strtoint(c4[2]);
        end;
        total:= total + soma;

        if num = 2 then
            num:= 1
        else
            num:= 2;
    end;
    res:= total mod 10;
    if res <> 0 then
        res:= 10 - res;
    if res <> i3 then
    begin
        validaCMC7:= false;
    end;
end;

Obrigado

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...