DEV Community

Cover image for python validators
Keerthana M
Keerthana M

Posted on

python validators

Euclidean Algorithm

  • In mathematics, the Euclidean algorithm or Euclid's algorithm, is an efficient method for computing the **greatest common divisor (GCD) **of two integers, the largest number that divides them both without a remainder.

  • Simple method to find the HCF/GCD (Highest Common Factor) of two numbers

def hcf(a, b):
    while b != 0:
        a, b = b, a % b
    print("HCF =", a)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
hcf(a, b)
Enter fullscreen mode Exit fullscreen mode

Output:
Enter first number: 12
Enter second number: 24
HCF = 12

Luhn Algorithm - Credit Card Validation

  • The Luhn algorithm (also called the modulus 10 or mod 10 algorithm) is a checksum method developed by Hans Peter Luhn to validate identification numbers such as credit card numbers, IMEI numbers, and other numeric identifiers.
  • It’s widely used in payment systems to detect common input errors before processing transactions.
def check(card):
    total = 0
    count = 0
    while card > 0:
        digit = card % 10
        if count % 2 == 1:
            digit = digit * 2
            if digit > 9:
                digit = digit - 9
        total = total + digit
        card = card // 10
        count = count + 1
    if total % 10 == 0:
        print("Valid")
    else:
        print("Invalid")
card = int(input("Enter card number: "))
check(card)
Enter fullscreen mode Exit fullscreen mode

Output:
Enter card number: 1234567890
Invalid

Pan Card Validation

def pan_check(pan):
    if len(pan) != 10:
        print("Invalid PAN")
        return
    i = 0
    while i < 5:
        if not pan[i].isalpha():
            print("Invalid PAN")
            return
        i += 1
    i = 5
    while i < 9:
        if not pan[i].isdigit():
            print("Invalid PAN")
            return
        i += 1
    if not pan[9].isalpha():
        print("Invalid PAN")
    else:
        print("Valid PAN")
pan = input("Enter PAN: ")
pan_check(pan)
Enter fullscreen mode Exit fullscreen mode

Output:
Enter PAN: ABCDE1234F
Valid PAN

Aadhaar Card Validation

Aadhaar card validation ensures that your 12-digit Aadhaar number is correctly formatted and officially active in the UIDAI database.
Official Aadhaar Validation

  1. Select 'Verify Aadhaar Number' under Aadhaar Services.
  2. Enter your 12-digit Aadhaar number and the security code.
  3. Click 'Verify' If your Aadhaar is valid and active, the system will confirm its status. If it is deactivated or invalid, an error message will appear
  4. Official validation confirms that your Aadhaar is recognized by UIDAI and can be used for government services, banking
def aadhaar_check(aadhaar):
    if len(aadhaar) != 12:
        print("Invalid Aadhaar")
        return
    count = 0
    while count < 12:
        if not aadhaar[count].isdigit():
            print("Invalid Aadhaar")
            return
        count += 1
    print("Valid Aadhaar")
aadhaar = input("Enter Aadhaar number: ")
aadhaar_check(aadhaar)
Enter fullscreen mode Exit fullscreen mode

Output:
Enter Aadhaar number: 123456789045
Valid Aadhaar

Top comments (0)