Spaces:
Sleeping
Sleeping
File size: 1,027 Bytes
b407a42 91dc3b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import re
def is_email(identifier: str) -> bool:
return re.match(r"[^@]+@[^@]+\.[^@]+", identifier) is not None
def is_phone(identifier: str) -> bool:
"""
Validate phone number format. Supports:
- International format: +1234567890, +91-9876543210
- National format: 9876543210, (123) 456-7890
- With/without spaces, dashes, parentheses
"""
# Remove all non-digit characters except +
cleaned = re.sub(r'[^\d+]', '', identifier)
# Check if it's a valid phone number (8-15 digits, optionally starting with +)
if re.match(r'^\+?[1-9]\d{7,14}$', cleaned):
return True
return False
def validate_identifier(identifier: str) -> str:
"""
Validate and return the type of identifier (email or phone).
Raises ValueError if neither email nor phone format.
"""
if is_email(identifier):
return "email"
elif is_phone(identifier):
return "phone"
else:
raise ValueError("Identifier must be a valid email address or phone number") |