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")