How to validate a digitized line of billets in the format 00000.00000 00000.000000 00000.000000 0 00000000000000?

Asked

Viewed 3,354 times

2

Good afternoon friends,

by chance someone would have some procedure similar to that and that you could post here?

It will be of great use for me and future readers of this post.

I await return.

This was my last attempt to translate this Delphi code, but it’s wrong.

private static bool Ok = true;

    public static string RemoveFirst(this string source, string remove)
            {
                int index = source.IndexOf(remove);
                return (index < 0)
                    ? source
                    : source.Remove(index, remove.Length);
            }

            /// <summary>
            /// Valida o dígito verificador da linha
            /// </summary>
            /// <param name="chaveAcesso"></param>
            /// <returns></returns>

    public static string RetornaDigitoVerificadorCteModulo11(string chaveAcesso)
            {

                int peso = 2;
                int soma = 0;

                try
                {
                    chaveAcesso.ToCharArray()
                        .Reverse()
                        .ToList()
                        .ForEach(f =>
                        {
                            soma += (Convert.ToInt32(f.ToString()) * peso);
                            peso = (peso == 9) ? 2 : peso + 1;
                        });

                    return (11 - (soma % 11)) <= 1 ? "0" : (11 - (soma % 11)).ToString();
                }
                catch
                {
                    return "ERRO: A chave de acesso deve conter apenas números.";
                }
            }

            /// <summary>
            /// Valida de forma generalizada o DV da linha digitável
            /// </summary>
            /// <param name="linhaDigtavel"></param>
            /// <returns></returns>

            private static bool ValidacaoGeralDV(string linhaDigtavel)
            {
                string Dv;
                string DvVerificado;

                Dv = linhaDigtavel;

                RemoveFirst(Dv, " ");
                RemoveFirst(Dv, " ");
                RemoveFirst(Dv, " ");

                Dv = Dv.Substring(0, Dv.Length);
                DvVerificado = RetornaDigitoVerificadorCteModulo11(linhaDigtavel);

                return (DvVerificado == Dv) ? true : false;

            }

            /// <summary>
            /// Validação geral da linha digitável
            /// </summary>
            /// <param name="linha"></param>
            /// <returns></returns>

            private static void ValidacaoGeralLinha(string linha)
            {

                char[] caracteresLinha = linha.ToCharArray();


                bool correto =  (caracteresLinha[6] == '.') && 
                                (caracteresLinha[12] == ' ') &&
                                (caracteresLinha[18] == '.') &&
                                (caracteresLinha[25] == ' ') &&
                                (caracteresLinha[31] == '.') &&
                                (caracteresLinha[38] == ' ') &&
                                (caracteresLinha[40] == ' ');

                if (correto)
                {
                    for (int i = 1; i <= 54; i++)
                    {
                        if ( (i != 6) &&
                           (i != 12) &&
                           (i != 18) &&
                           (i != 25) &&
                           (i != 31) &&
                           (i != 38) &&
                           (i != 40) )
                        {
                            if (!linha.Contains(i.ToString()))
                            {

                                Ok = false;
                                break;
                            }


                        }

                    }

                }

            }

            /// <summary>
            /// Retorna a linha digitável já validada no formato: 00000.00000 00000.000000 00000.000000 0 00000000000000
            /// </summary>
            /// <param name="dado"></param>
            /// <returns></returns>

            private static string RetornaLinhaValidada(string dado)
            {
                string resultado = "";

                for (int i = 1; i <= dado.Length; i++)
                {

                    resultado = dado.Substring(0, dado.Length);


                    ValidacaoGeralLinha(resultado);

                    if (Ok)
                    {
                        if (ValidacaoGeralDV(resultado))

                            break;

                    }

                }

                return "";
            }

            static void Main(string[] args)
            {

                Console.WriteLine(RetornaLinhaValidada("10490.05539 03698.700006 00091.449587 5 55490000028531"));
                Console.ReadKey();

            }
  • 4

    Each bank has a different way, has to ask for their documentation. In fact, I think it’s not a matter of programming itself.

  • 1

    Developing this will be your least problem. As already stated by the bigown, each bench has a different pattern. You need to have the bank’s documentation to work on this.

  • 1

    You want to validate only the format or you want to validate the code?

  • The Randrade format as well as the Delphi code.

  • Let’s go from the top @Jonassilva. What exactly do you need to do? Do you want to pass the digitable line and validate exactly what?

  • I think this library can help you: https://github.com/BoletoNet/boletonet

  • @jbueno, I want to validate check digit and line format for the pattern in question.

  • 1

    @Jonassilva If you want to validate the checker digit, you need an implementation for each bank. It is not possible to make an implementation that encompasses all banks.

  • @jbueno, the famous Módulo11, would no longer be a "global" validation regardless of which bank is?

  • @jbueno, including this function there of Module 11 in Delphi returns the correctly independent digit of the database :D

Show 5 more comments

1 answer

6


If you want to validate only the format, here is a solution with Regular Expression:

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {       
        Console.WriteLine(ValidarLinhaDigitavel("00000.00000 00000.000000 00000.000000 0 00000000000000"));
    }

    public static bool ValidarLinhaDigitavel(string input)
    {
        return Regex.IsMatch(input, @"\d{5}\.\d{5} \d{5}\.\d{6} \d{5}\.\d{6} \d \d{14}");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.