Giter Club home page Giter Club logo

portugueseid_nif's Introduction

Portuguese ID and NIF validation checker

  • This was part of a project when I was student at University of The Azores.
  • The code works with python 3.6
  • The code may contain portuguese language
  • The Python file contains a GUI, created with the library Tkinter.

The portuguese ID, known in Portugal as Cartão de cidadão, is the first part of the code.
The algorithm is in the pdf file along with this post and has an example code in C#. Since we were learning Python 3, we had to translate the C# code into pyhton. if you want only the function I create to validate the ID/CC, you can copy the following code:

    def verificacao():
        listaalfa = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
        listanum = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]        
         
        cc1 = input()
        cc = cc1.lower()
        print(cc)
        soma = 0
        if len(cc) != 12:
            print('numero de cartao de cidadao invalido')
        else:
            for i in range (len(cc)-1,-1,-1):
                if i % 2 == 0:
                    if cc[i] in listaalfa:
                        pos = listaalfa.index(cc[i])
                        soma +=  listanum[pos]*2
                        if listanum[pos]*2 >= 10:
                            soma -= 9            
                else:
                    if cc[i] in listaalfa:
                        pos = listaalfa.index(cc[i])
                        soma +=  listanum[pos]
                            
            soma = soma % 10
            
            if soma == 0:
                print('cartao de cidadao valido')
            else:
                print('cartao de cidadao invalido')
     

The NIF, known in Portugal as Numero de Identificação Fiscal, or Fiscal Code, was made with the guide of the portuguese wikipedia (https://pt.wikipedia.org/wiki/N%C3%BAmero_de_identifica%C3%A7%C3%A3o_fiscal).
if you want only the python code:

    def verificacao():
        nif = input()
        
        soma = 0
        verificacao = 0
        if len(nif) != 9:
            print('invalido')
            return
        index = 7
        for i in range(2,10):
            soma += int(nif[index])*i
            index -= 1
        verificacao = soma % 11
        if verificacao == 0 or verificacao == 1:
            verificacao = 0
        else:
            verificacao = 11 - verificacao
        if verificacao == int(nif[8]):
            print('valido')
        else:
            print('invalido')
                

portugueseid_nif's People

Contributors

jpfr12 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.