Mobile Number Verify
Source: Dev.to
Overview
A simple Python function that validates Indian mobile numbers based on allowed characters and specific digit rules.
Validation Rules
Condition 1 – Allowed characters
Only digits (0‑9), the plus sign (+), or a space ( ) are permitted. Any other character causes the function to reject the input.
Condition 2 – 10‑digit numbers
If the input length is 10, the first digit must be between 6 and 9 (inclusive).
Condition 3 – Numbers with country code +91 (no space)
If the input length is 13, it must start with +91. The fourth digit (index 3) must be between 6 and 9.
Condition 4 – Numbers with country code +91 followed by a space
If the input length is 14, it must start with +91 (including the trailing space). The fifth digit (index 4) must be between 6 and 9.
Function Implementation (Python)
def mobile(n):
# Condition 1 – allowed characters
i = 0
while i < len(n):
if ("0" <= n[i] <= "9") or n[i] == "+" or n[i] == " ":
i += 1
else:
return "characters and special characters not allowed"
# Condition 2 – 10‑digit format
if len(n) == 10:
if "6" <= n[0] <= "9":
return "valid"
return "First number should not be less than 6"
# Condition 3 – +91 format (no space)
if len(n) == 13:
if n[:3] == "+91":
if "6" <= n[3] <= "9":
return "valid"
return "Fourth number should not be less than 6"
return "First three numbers should be +91"
# Condition 4 – +91 format with trailing space
if len(n) == 14:
if n[:4] == "+91 ":
if "6" <= n[4] <= "9":
return "valid"
return "Fifth number should not be less than 6"
return "First four numbers should be +91 "
return "Please enter a valid mobile number"
Usage
print(mobile(input("Enter your mobile number: ")))