DEV Community

Cover image for Input Validation in Java: Validating PIN Code, Gmail, and Mobile Numbers
Arul .A
Arul .A

Posted on

Input Validation in Java: Validating PIN Code, Gmail, and Mobile Numbers

1.pincode validation :

  • Must be 6 digits
  • Only numbers (no letters, no symbols)
  • first digit can’t be 0
def pincode(pc):
    if pc//100000!=0 and pc//100000<=9:
        print("valid")
    else:
        print("invalid")
pincode(int(input("Enter your 6 digit pin code :")))

Enter fullscreen mode Exit fullscreen mode

2.Gmail validation :

  • Allow only characters: a–z, A–Z, 0–9, '@', '.', '_'.
  • If any other character appears → invalid.
  • Email must end with "@gmail.com".
  • There must be at least one character before "@gmail.com".
  • Special characters (@ . _) are allowed anywhere (no position check).
  • If all above conditions pass → valid Gmail, else invalid.
def verify_Gmail(Gmail):
    i=0
    while i<len(Gmail):
        current=Gmail[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=="." or current=="_":
            continue;
        else:
            return False
    if Gmail[-10:]=="@gmail.com":
        return True
    else:
        return False
if verify_Gmail(input("Enter Your Gmail :")):
    print("valid")
else:
    print("invalid")
Enter fullscreen mode Exit fullscreen mode

3.Mobile number validation :

  • Allow only digits (0–9), '+' and space; reject anything else.
  • If length = 10 → must be all digits and start with 6–9.
  • If length = 13 → must start with "+91" and next digit must be 6–9.
  • If length = 14 → must start with "+91 " and next digit must be 6–9.
  • Otherwise → invalid mobile number.
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

Top comments (0)