Saturday, 22 August 2026

๐Ÿš€ Day 103/150 – Phone Number Validation in Python

 




๐Ÿš€ Day 103/150 – Phone Number Validation in Python

Validating phone numbers is one of the most common tasks in Python applications. Whether you're building a registration form, login system, or contact management app, ensuring users enter a valid phone number helps improve data accuracy and prevents invalid entries.

In this post, we'll explore four different ways to validate phone numbers in Python, starting from simple checks to a more reliable solution using Regular Expressions.


Method 1 – Basic Phone Number Validation


The easiest way to validate a phone number is by checking whether it contains exactly 10 characters.


phone = input("Enter your phone number: ") if len(phone) == 10: print("Valid Phone Number") else: print("Invalid Phone Number")






Sample Input
9876543210

Output
Valid Phone Number

Explanation
input() accepts the phone number from the user.
len() calculates the total number of characters.
If the length is exactly 10, the number is considered valid.
Otherwise, it is marked as invalid.

Note: This method only checks the length and does not verify whether all characters are digits

Method 2 – Check Digits Only

A valid phone number should contain only numeric digits.

phone = input("Enter your phone number: ") if phone.isdigit(): print("Valid Phone Number") else: print("Invalid Phone Number")




Sample Input

9876543210

Output

Valid Phone Number

Explanation

isdigit() checks whether every character in the string is a digit.

If all characters are numeric, it returns True.

Otherwise, the phone number is considered invalid.

Note: This method doesn't verify the length of the phone number.


Method 3 – Check Length and Digits

This method combines the previous two validations to make the check more reliable.

phone = input("Enter your phone number: ") if len(phone) == 10 and phone.isdigit(): print("Valid Phone Number") else: print("Invalid Phone Number")




Sample Input

9876543210

Output

Valid Phone Number

Explanation

len(phone) == 10 ensures the phone number contains exactly 10 characters.

phone.isdigit() confirms every character is a digit.

Both conditions must be true for the phone number to be valid.

This approach is commonly used in beginner-level Python programs.


Method 4 – Using Regular Expressions

Regular Expressions (Regex) provide a more professional and flexible way to validate phone numbers.

import re phone = input("Enter your phone number: ") pattern = r"^[0-9]{10}$" if re.match(pattern, phone): print("Valid Phone Number") else: print("Invalid Phone Number")






Sample Input

9876543210

Output

Valid Phone Number

Explanation

import re imports Python's Regular Expression module.

^[0-9]{10}$ means:

^ → Start of the string

[0-9] → Any digit from 0 to 9

{10} → Exactly 10 digits

$ → End of the string

re.match() checks whether the entire input matches the pattern.

Regex is widely used in real-world applications because it provides accurate validation with minimal code.


Comparison of Methods

Method Best For

Check Length Basic validation

Check Digits Only Ensuring numeric input

Check Length + Digits Beginner-friendly phone validation

Regular Expressions Professional and production-level validation


๐Ÿ”ฅ Key Takeaways

Phone number validation helps prevent invalid user input.

len() checks whether the phone number has the required number of characters.

isdigit() ensures every character is numeric.

Combining length and digit validation provides a better solution.

Regular Expressions (re) offer the most reliable and scalable validation approach.

Phone number validation is commonly used in registration forms, authentication systems, contact applications, and web development projects.

Stay tuned for Day 104 of the #150DaysOfPython series! ๐Ÿš€


Learn :

Data Structures and Algorithm Design using Python











0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (337) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (337) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (88) Coursera (302) Cybersecurity (34) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (420) Data Strucures (18) Deep Learning (215) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (387) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1360) Python Coding Challenge (1223) Python Library (1) Python Mathematics (11) Python Mistakes (51) Python Quiz (607) Python Tips (101) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (19) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)