Aadhar Card Number Validation In Python

If you are looking for a tutorial on Aadhar card validation in python programming then today in this python tutorial I will show you how to do Aadhar card number validation in python programming.
If you are creating a python project and you want to add an Aadhar card validation feature to it then you are at the right place, I will show you the easiest way to do it.
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 our python program to validate a Aadhar card number. Now let’s see it in code.
Aadhar Card Validation In Python Code
# 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 in python programming. You can run this code in an online compiler or on your computer to test.
# 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.
Summary
This was the python tutorial on Aadhar card validation in python programming. 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:
- Python program to check even or odd numbers.
- Python program to wish happy birthday.
- Python program to validate mobile number.
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 🙂