Validate Phone Number Using Regex in Python
Validate a simple phone number format using regular expressions.
IntermediateString ProgramsExample 23 of 25
validate-phone-number-using-regex.py
Run in browser1# Program to validate phone number using regex23import re45phone = input("Enter phone number: ")67pattern = r"^\+?\d{10,15}$"89if re.match(pattern, phone):10 print("Valid phone number")11else:12 print("Invalid phone number")
Output
Enter phone number: +911234567890 Valid phone number
What's going on
We allow an optional + followed by 10 to 15 digits to represent an international-style phone number.