DEV Community

Cover image for Pincode, Phone number, and Gmail Validation Programs in Python
Sasireka
Sasireka

Posted on

Pincode, Phone number, and Gmail Validation Programs in Python

1) Pincode Validation

regions = {
    1: "Delhi",
    2: "Uttar Pradesh & Uttarakhand",
    3: "Rajasthan & Gujarat",
    4: "Maharashtra",
    5: "Andhra Pradesh & Telangana",
    6: "Tamil Nadu & Kerala",
    7: "West Bengal & North East",
    8: "Bihar & Jharkhand",
    9: "Army Postal Service"
}

sub_regions = {
    11: "Delhi Region",
    40: "Mumbai Region",
    60: "Chennai Region",
    70: "Kolkata Region"
}

districts = {
    110: "Delhi",       111: "Delhi (North)",      112: "Delhi (South)",
    400: "Mumbai",      401: "Mumbai (Suburban)",   402: "Mumbai (West)",
    600: "Chennai",     601: "Tiruvallur",          602: "Kanchipuram",
    700: "Kolkata",     701: "Kolkata (North)",     702: "Kolkata (South)"
}

def get_pincode_details(pincode):
    if not pincode.isdigit() or len(pincode) != 6:
        print("Invalid pincode. Enter exactly 6 digits.")
        return

    num         = int(pincode)
    first_digit = num // 100000
    first_two   = num // 10000
    first_three = num // 1000

    print("Region:     ", regions.get(first_digit,   "Unknown"))
    print("Sub-region: ", sub_regions.get(first_two, "Unknown"))
    print("District:   ", districts.get(first_three, "Unknown"))

pincode = input("Enter Pincode: ").strip()
get_pincode_details(pincode)
Enter fullscreen mode Exit fullscreen mode

Output:

2) Phone Number Validation

def verify(mobnum):
    index=0
    while index<len(mobnum):
        if mobnum[index]>="0" and mobnum[index]<="9":
            index+=1
            continue;
        else:
            return False
    return True



def mobile(n):
    i=0
    while i<len(n):
        if(n[i]>="0" and n[i]<="9") or (n[i]=="+") or (n[i]==" "):
            i+=1
        else:
            return "characters and special charactors not allowed"

    if len(n)==10:
        if n[0]>="6" and n[0]<="9" and verify(n):
            return "valid";
        else:
            return "incorrect"

    if len(n)==13:
        if n[0:3]=="+91":
            if n[3]>="6" and n[3]<="9" and verify(n[3:]):
                return "valid"
            else:
                return "incorrect"
        return "First three number should like +91"

    if len(n)==14:
        if n[0:4]=="+91 ":
            if n[4]>="6" and n[4]<="9" and verify(n[4:]):
                return "valid"
            else:
                return "incorrect"
        return "First four number should like +91 "
    return "Please Enter Your valid Mobile Number"      
print(mobile(input("Enter Your Mobile  Number : ")));
Enter fullscreen mode Exit fullscreen mode

Output:

3) Gmail Validation

def verify_Gmail(Gmail):
    if Gmail[-10:]=="@gmail.com":
        i=0
        gmailcopy=Gmail[0:len(Gmail)-10]
        while i<len(gmailcopy):
            current=gmailcopy[i]
            i+=1
            if current>="a" and current<="z":
                continue;
            elif current>="A" and current<="Z":
                continue;
            elif current>="0" and current<="9":
                continue;
            elif current=="." or current=="_":
                continue;
            else:
                return False
        return True
    else:
        return False

if verify_Gmail(input("Enter Your Gmail :")):
    print("valid")
else:
    print("invalid")
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)