Explore

Close
Python Programs Share

Mobile Number Validation In Python

Mobile Number Validation In Python

Today in this python tutorial I will show you mobile number validation in python programming. I will show you step by step how to validate a mobile number in python, so read this guide till the end.

If you are creating a python project and you want to add mobile number validation feature to it then you are at the right place, I will show you two ways of mobile number validation in python 

You can use any of the above ways you find easy to validate mobile numbers in python programming.

Here are some of the Mobile number validation criteria:

  • The mobile number should be 10 digit only
  • The mobile number should only start with 7 or 8 or 9
  • The mobile number should not contain any characters or strings

Above are all the criteria we need to use to validate a mobile number. Note that I have used the Indian mobile number validation criteria. You can modify the code easily to meet your country criteria.

Mobile Number Validation In Python Using If-Else


num = input("Please enter a 10 digit mobile number: ")

# Check if number length is greater or less then 10
if len(num) > 10 or len(num) < 10:
    print("Number is not valid (Enter a 10 digit number)")
else:
    # Check if first number is 7 or 8 or 9
    if num[0] == '7' or num[0] == '8' or num[0] == '9':
        try:
            num = int(num)
            print("Your number is valid")
        except:
            print('Number is not valid (A number should not contain any characters)')    
    else:
        print("Number is not valid (A number should start with 7 or 8 or 9)")

Above is the code to validate a mobile number in python using nested if-else. You can run the above code in an online compiler or on your computer to test.


# Check if number length is greater or less then 10
if len(num) > 10 or len(num) < 10:
    print("Number is not valid (Enter a 10 digit number)")
else:

In the first if else we check if the number is greater or less than 10 if it is not then we print the error message and if it is exact 10 digits we go to the next step in validation


 # Check if first number is 7 or 8 or 9
    if num[0] == '7' or num[0] == '8' or num[0] == '9':

If we successfully pass the 10 digit mobile number validation we check if the first digit in the number is 7 or 8 or 9 if it is not then we throw an error message and if it is then next validation


try:
    num = int(num)
    print("Your number is valid")
except:
    print('Number is not valid (A number should not contain any characters)')

Now here I have used to try and except in the try I am converting the num to int if it fails to convert because of any characters in number then it will go in the except where the error message will show.

If there are no errors and successfully converts into int, then it will show number is successfully validated like this:


>> Please enter a 10 digit mobile number: 9155713160
>> Your number is valid

Above was the way to do mobile validation using if else now let's see how to do mobile validation in python using regular expressions.

Mobile Number Validation In Python Using Regex


import re

num = input('Enter a mobile number to validate: ')

Pattern = re.compile("[7-9][0-9]{9}")

if Pattern.match(num):
    print('Valid Mobile Number')
else:
	print("Invalid Mobile Number")

Above is the code to do mobile validation in python using regex also known as regular expression as you can see the above code is just 7 lines.

This way of doing mobile validation is very suitable and easy compared to the first way which I have shown.

The code is short and simple because of regex. Everything we have done using if else in the first way is done in just one line of code of regex.


Pattern = re.compile("[7-9][0-9]{9}")

If the regex pattern matches then it will print that the mobile number is validated and if not matches then not validated.


>> Enter a mobile number to validate: 9727713110
>> Valid Mobile Number

I will recommend you to use this way to validate mobile numbers because it looks clean and simple.

Summary

This was the python tutorial on mobile number validation in python programming. I hope you found this tutorial helpful and you were able to successfully implement the mobile number validation feature.

Here are some more python guides you may find useful:

I hope you found what you were looking for and if you want more python tutorials like this do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂