Validação do número de contribuinte

bandalho_zune

I'm cool cuz I Fold
Aqui está uma pequena função que valida o número de contribuinte, escrita em C# que implementei num projecto a uns tempos.

Cumps.
Código:
 public bool IsValidContrib(String contrib)
   {
       String s;
       char c;
       int checkDigit;

       s = contrib;

       if (s.Length == 9)
       {
           c = s[0];
           //Digitos iniciais válidos
           if (c == '1' || c == '2' || c == '5' || c == '6' || c == '8' || c == '9') 
           {
               checkDigit = (c - '0') * 9;

               for (int i = 2; i <= 8; i++)
                   checkDigit += (s[i - 1] - '0') * (10 - i);

               checkDigit = 11 - (checkDigit % 11);

               if (checkDigit >= 10)
                   checkDigit = 0;

               if (checkDigit == (s[8] - '0'))
                   return true;
           }
       }

       return false;
   }
 
Última edição:
Back
Topo