Pythondex
Python Programs

Aadhar Card Number Validation In Python

Last updated April 24, 2023 by Jarvis Silva

If you are looking for a tutorial on Aadhar card validation in python programming then you are at the right place today in this tutorial we will see how to validate a aadhar card using python.

Validating aadhar card number is very important to check if person has given a valid aadhar card number or else the user can enter any random numbers.

Here are some of the Aadhar card number validation criteria we have to follow:

  • Aadhar Number should not start with 0 or 1
  • Aadhar number should be 12 digit only.
  • Aadhar number should not contain alphabets.

By using the above criterias we will create this python program.

Python code to validate aadhar card number


# Number should be 12 chars
# Number should not start with 0 or 1
# It should not contain alphabets

num = input("Please enter a 12 digit mobile number: ")

# Check if number length is greater or less then 12
if len(num) > 12 or len(num) < 12:
    print("Aadhar number is not valid (Enter a 12 digit number)")
else:
    # Check if first number is 0 or 1
    if num[0] == '0' or num[0] == '1':
        print("Aadhar number is not valid (Aadhar card number cannot start with 0 or 1)")
    else:
        try:
            num = int(num)
            print("Aadhar number is valid")
        except:
            print('Aadhar number is not valid (Aadhar number should not contain any characters)')    


Above is the code to validate aadhar card number. You can run this code in an online compiler or on your computer, so let's see how this program validates aadhar card.


# Check if number length is greater or less then 12
if len(num) > 12 or len(num) < 12:
    print("Aadhar number is not valid (Enter a 12 digit number)")
else:

In the first if-else statement we check the length of the number if it is less or greater than 12 then it prints it is not valid and if it is 12 digit it will go to the next step of validation.


    # Check if first number is 0 or 1
    if num[0] == '0' or num[0] == '1':
        print("Aadhar number is not valid (Aadhar card number cannot start with 0 or 1)")
    else:

After checking the length of the number we check if the first digit of the aadhar number is 0 or 1 if it is not we move to the next step of validation.


       try:
            num = int(num)
            print("Aadhar number is valid")
        except:
            print('Aadhar number is not valid (Aadhar number should not contain any characters)')    

Now the final step is to check if it contains any alphabets, so to do that I have used try and except statement in the try statement it will try to convert the number into integer.

If it successfully converts the number it will print aadhar number is validated like below, if it does not get converted it will throw an error and except will run.


Please enter a 12 digit mobile number: 874878388383
Aadhar number is valid         

Above is the output of this program, it will show when the aadhar card number is successfully validated.

We successfully validated aadhar card number in python, I hope you found this tutorial helpful and you were able to successfully implement the aadhar number validation feature.

Here are some more python guides you may find useful:

I hope you found what you were looking for and if you want more python tutorials like this do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂