Skip to content
Laviyx Learn

Try

Python Lab · Experiment 30

Validate a phone number and an email id

PythonWeek 6JNTUH R22 · CS206ES

Aim

To write a Python program that reads a mobile phone number and an email id from the user and checks, using regular expressions, whether each one is written in a correct format.

Algorithm

  1. Write the pattern for an Indian mobile number: an optional +91 (with an optional space or hyphen after it), then exactly 10 digits, the first of which is 6, 7, 8 or 9.
  2. Write the pattern for an email id: a user name of letters, digits and . _ % + -, then @, then a domain of one or more parts made of letters, digits and hyphens, separated by dots, and ending in a dot and at least 2 letters.
  3. Read the phone number and the email id, and remove spaces at the ends with strip().
  4. Check the phone number with re.fullmatch. Print whether it is valid; if not, print the rule it must follow.
  5. Check the email id in the same way and print the result.

Program

Python
import re

PHONE_PATTERN = r"(\+91[ -]?)?[6-9][0-9]{9}"
EMAIL_PATTERN = r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}"


def is_valid_phone(phone):
    return re.fullmatch(PHONE_PATTERN, phone) is not None


def is_valid_email(email):
    return re.fullmatch(EMAIL_PATTERN, email) is not None


phone = input("Enter mobile number: ").strip()
email = input("Enter email id: ").strip()

if is_valid_phone(phone):
    print(phone, "is a valid mobile number")
else:
    print(phone, "is NOT a valid mobile number")
    print("  Rule: 10 digits starting with 6, 7, 8 or 9, optionally preceded by +91")

if is_valid_email(email):
    print(email, "is a valid email id")
else:
    print(email, "is NOT a valid email id")
    print("  Rule: [email protected], for example [email protected]")

Output

Valid phone number and email id

Output 1
Enter mobile number: +91 9876543210
Enter email id: [email protected]
+91 9876543210 is a valid mobile number
[email protected] is a valid email id

Invalid phone number and email id

Output 2
Enter mobile number: 5876543210
Enter email id: ravi.kumar@example
5876543210 is NOT a valid mobile number
  Rule: 10 digits starting with 6, 7, 8 or 9, optionally preceded by +91
ravi.kumar@example is NOT a valid email id
  Rule: [email protected], for example [email protected]

Recorded by running this exact program. Typed input appears after each prompt.

How it works

The rules checked here are:

  • Mobile number: an optional country code +91, which may be followed by one space or hyphen, then exactly 10 digits starting with 6, 7, 8 or 9 (Indian mobile numbers start with these digits). Landline numbers with an STD code are not accepted.
  • Email id: one or more letters, digits or the characters . _ % + -, then @, then a domain such as example.com or example.co.in whose last part has at least 2 letters.

The pattern parts used: [6-9] is one digit from 6 to 9, [0-9]{9} is exactly 9 more digits, ( ... )? makes a group optional, \+ is a real plus sign (a bare + means "one or more"), \. is a real dot (a bare . means "any character"), and {2,} means 2 or more.

re.fullmatch succeeds only if the pattern matches the whole string. re.match checks only the start, so 9876543210abc would wrongly pass with it.

In the invalid run, 5876543210 fails because it starts with 5, and ravi.kumar@example fails because the domain has no dot and extension. These are format checks only; they cannot tell whether the number or the mailbox actually exists. The full email standard allows more forms than this pattern, but this pattern accepts the addresses people normally use.

Common mistakes

  • Using re.match or re.search instead of re.fullmatch without ^ and $. Then extra characters before or after the number are accepted, such as 98765432101234.

  • Writing . for a dot in the email pattern. A bare . matches any character; a real dot must be written as \..

  • Forgetting the r before the pattern string. Without a raw string, backslashes such as \. may be treated as string escapes and give warnings or wrong patterns.

  • Not removing spaces with strip(). A space typed at the end makes a correct number fail the check.

Viva questions

Try to answer each one aloud before you open it.

  • What is a regular expression?

    A pattern that describes a set of strings, used to search, match or check text. In Python, regular expressions are used through the re module.

  • What is the difference between re.match, re.search and re.fullmatch?

    re.match checks for the pattern only at the start of the string, re.search looks for it anywhere in the string, and re.fullmatch succeeds only if the whole string matches the pattern.

  • What do the symbols ?, + and * mean in a pattern?

    ? means zero or one time, + means one or more times, and * means zero or more times, applied to the item just before them.

  • What does [6-9][0-9]{9} match?

    One digit from 6 to 9 followed by exactly nine digits, so a 10-digit number that starts with 6, 7, 8 or 9.

  • Why are raw strings (r"...") used for regular expressions?

    In a raw string the backslash is kept as it is, so \d or \. reaches the re module unchanged instead of being treated as a string escape.

Learn the concepts

All Python Programming Lab experiments →