Validate mobile number in Python
Consider we are validating an Indian mobile number it has at most 13 digits with +91 before the 10 digit number which starts with the starting number between 7-10.
If the user enters the 10-digit number for which the starting number is between 7-10 the number is treated as valid.
The code below is written in such a way that it gives us a valid output if it is a 13-digit number that starts with +916,+917,+918, or +919 as well as if the number consists of 10 digits starting with 6,7,8 or 9.
def validate_phone(number):
"""
Validate a phone number.
A valid phone number:
- Can be a 13-digit number starting with "+916", "+917", "+918", or "+919"
- Can be a 10-digit number starting with 6, 7, 8, or 9
Args:
number (str): The phone number to validate.
Returns:
str: "Valid" if the number is valid, otherwise "Invalid".
"""
# Check if the number is all digits or starts with '+' and the rest are digits
if number.isdigit() or (number.startswith('+') and number[1:].isdigit()):
# Check if the number is a 13-digit number starting with "+916", "+917", "+918", or "+919"
if len(number) == 13 and (number.startswith("+916") or number.startswith("+917") or number.startswith("+918") or number.startswith("+919")):
return "Valid"
# Check if the number is a 10-digit number starting with 6, 7, 8, or 9
elif len(number) == 10 and number[0] in "6789":
return "Valid"
# If none of the conditions are met, return "Invalid"
return "Invalid"
# Example usage
if __name__ == "__main__":
phone_input = input("Enter the phone number: ")
print(validate_phone(phone_input))
1.Function Definition and Docstring:
- The function
validate_phoneis defined to take one parameter:number. - A docstring is provided to explain the purpose of the function, the expected argument, and the return value.
2.Check for Valid Digits:
- The
if number.isdigit() or (number.startswith('+') and number[1:].isdigit()):line checks if the inputnumberconsists entirely of digits or if it starts with a ‘+’ followed by digits. number.isdigit()ensures the number is all digits.(number.startswith('+') and number[1:].isdigit())allows for a ‘+’ at the beginning and checks if the rest of the string is digits.
3.Check for 13-Digit Number with Specific Prefixes:
if len(number) == 13 and (number.startswith("+916") or number.startswith("+917") or number.startswith("+918") or number.startswith("+919")):len(number) == 13checks if the length of the number is 13 characters.number.startswith("+916") or ...ensures the number starts with one of the specified prefixes: “+916”, “+917”, “+918”, or “+919”.- If both conditions are met, the function returns “Valid”.
4.Check for 10-Digit Number Starting with 6, 7, 8, or 9:
- elif len(number) == 10 and number[0] in “6789”:
len(number) == 10checks if the length of the number is 10 characters.number[0] in "6789"ensures the first digit is one of 6, 7, 8, or 9.- If both conditions are met, the function returns “Valid”.
5.Return “Invalid” for Non-Matching Numbers:
- If none of the conditions are met, the function returns “Invalid”.
6.Example Usage:
- The script includes a main guard:
if __name__ == "__main__":. - This allows the script to be run as a standalone program.
phone_input = input("Enter the phone number: ")prompts the user to input a phone number.print(validate_phone(phone_input))prints the result of thevalidate_phonefunction based on the user’s input.
Output:
Enter the phone number: +916784321021
Valid
Enter the phone number: 9347123482
Valid
Enter the phone number: 96388929aa
Invalid