Mobile Number validation Using Python

If you’re working on a Python project and want to add a mobile number validation feature, you should follow some validation criteria.

Mobile Number Validation Criteria:

  • The mobile number should only have 10 digits.
  • The mobile number should only begin with 7, 8, or 9 digits.
  • There should be no characters or strings in the mobile number.

All of the criteria listed above must be met in order to validate a mobile number.

NOTE: Here, we used the Indian mobile number validation requirements. You can easily edit the code to fit your country’s criteria.

We can Validate a mobile number using two methods:

  1. Using If-Else conditional statements.
  2. Using Regex

Mobile Number validation Using Python

Below are the ways to validate the given mobile number using python:

Method #1: Using If-Else conditional statements

Approach:

  • Give some random 10 digit mobile number as user input using the input() function and store it in a variable.
  • Check if the length of the given mobile number is greater than or less than 10 using the if conditional statement
  • If it is true then print “Invalid Mobile number. Mobile number must have 10 digits”
  • Else Check if the first digit of the given mobile number is 7 or 8 or 9 using the if conditional statement.
  • Handling the errors using the try-except blocks.
  • If it is true, then convert the given mobile number to an Integer using the int() function and store it in the same variable.
  • Print the given mobile number is a valid number
  • Print “Invalid Mobile number. Mobile number must not contain any characters”
  • Else print “Invalid Mobile number. Mobile number must begin with 7 or 8 or 9”.
  • The Exit of the Program.

Below is the implementation:

# Give some random 10 digit mobile number as user input using the input() 
# function and store it in a variable.
gvn_mobilenumb = input("Enter some random 10 digit mobile number = ")

# Check if the length of the given mobile number is greater than or less than 10
# using the if conditional statement
if len(gvn_mobilenumb) > 10 or len(gvn_mobilenumb) < 10:
    # If it is true then print "Invalid Mobile number. Mobile number must have 10 digits"
    print("Invalid Mobile number. Mobile number must have 10 digits")
else:
    # Else Check if first digit of the given mobile number is 7 or 8 or 9 using the if conditional statement
    if gvn_mobilenumb[0] == '7' or gvn_mobilenumb[0] == '8' or gvn_mobilenumb[0] == '9':
        # Handling the errors using the try-except blocks
        try:
            # If it is true, then convert the given mobile number to an Integer using the
            # int() function and store it in the same variable.
            gvn_mobilenumb = int(gvn_mobilenumb)
            # Print the given mobile number is a valid number
            print("The given mobile number is a Valid Number")
        except:
            # Print "Invalid Mobile number. Mobile number must not contain any characters"
            print('Invalid Mobile number. Mobile number must not contain any characters')
    else:
        # Else print "Invalid Mobile number. Mobile number must begin with 7 or 8 or 9"
        print("Invalid Mobile number. Mobile number must begin with 7 or 8 or 9")

Output:

Enter some random 10 digit mobile number = 9989593198
The given mobile number is a Valid Number
Enter some random 10 digit mobile number = 4567890182
Invalid Mobile number. Mobile number must begin with 7 or 8 or 9
Enter some random 10 digit mobile number = 9989583v99
Invalid Mobile number. Mobile number must not contain any characters

Method #2: Using Regex Module

fullmatch() function:
fullmatch() is a function that accepts two inputs: pattern and the string for validation.

If and only if the full string matches the pattern, it returns a match object; otherwise, it returns None.
We can determine whether or not the return value is a valid number based on the return value.

Mobile Number Validation Criteria:

  • The first digit should have values ranging from 6 to 9.
  • The remaining 9 numbers might be any integer between 0 and 9.

Example Pattern:

‘[6-9][0-9]{9}’: It means the starting should be between 6 to 9 and the next nine digits can be any number between 0 and 9.

‘[6-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]’: This pattern works in the same way as the above one.

Approach:

  • Import re(regex) module using the import keyword
  • Give some random mobile number as user input using the input() function and store it in a variable.
  • Pass the Pattern, given mobile number as arguments to the fullmatch() function to check if the entire
  • mobile number matches the pattern else it returns None.
  • Store it in a variable
  • Here the pattern represents that the starting number should be between 6-9 and the next nine digits can be any number between 0 and 9.
  • Check if the above result is not Equal to None Using the if conditional statement
  • If it is true, i.e the output is Not None then print “Valid Number”
  • Else print “Invalid Number”.
  • The Exit of the Program.

Below is the implementation:

# Import re(regex) module using the import keyword
import re
# Give some random mobile number as user input using the input() 
# function and store it in a variable.
gvn_mobilenumb = input("Enter some random mobile number = ")
# Pass the Pattern, given mobile number as arguments to the fullmatch() function to 
# check if the entire mobile number matches the pattern else it returns None.
# Store it in a variable
# Here the pattern represents that the starting number should be between 6-9
# and the next nine digits can be any number between 0 and 9.
rslt = re.fullmatch('[6-9][0-9]{9}',gvn_mobilenumb) 
# Check if the above result is not Equal to None Using the if conditional statement
if rslt!=None: 
     # If it is true, i.e the output is not None then print "Valid Number"
     print('The given mobile number is a Valid Number')
else:
     # Else print "Invalid Number"
     print('The given mobile number is Invalid')

Output:

Enter some random mobile number = 9494580181
The given mobile number is a Valid Number