10 Digit Mobile Number Validation in Python

Introduction:

In this article, we are discussing how to check 10 digit mobile number validation in Python. Here we are only checking the validation of 10 digit mobile number. The mobile number is 7894561230. There are some criteria for validating mobile number, which is -

  1. The first digit of the valid mobile number must start between 6 to 9.
  2. The rest digits of the valid mobile number must be starts between 0 to 9.
  3. When the +91 digits add at the front of any mobile number, then this is 12 digit mobile number, and it is also validating number.

When the 0 digits add at the front of any mobile number, then this is 12 digit mobile number, and it is also validating number.

Algorithm 1 of checking validation of mobile number:

Here we share the algorithm for checking the validation of mobile numbers. We have done it using Python programming language. In Python, there is various kind of modules. 're' module is one of them. This module is used for matching the pattern. The steps for checking the validation of the mobile number are given below -

  1. Firstly, import the 're' module in Python.
  2. Now we must create a function in the re module to check the validation of the mobile number. The function name is fullmatch.
  3. fullmatch function mainly takes two inputs. One input takes for the pattern, and for string validation, take another input.
  4. When the entire string matches the pattern, it returns the match object. Otherwise, it returns a None value. Depending on this value, we can decide whether the given mobile number is validated or not validated.
  5. We can write multiple patterns for the same validation of string. I will share some methods for this mobile number validation checking. The methods are given below -
  6. '[6-9][0-9]{9}' . This is a simple way the first digit of the validated mobile number must be started between 6 to 9 for any number, and the rest digits of the validated mobile number are must be started between 0 to 9 for any number.
  7. Another pattern is '[6-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'. It also works like the previous pattern.

Program code for checking the validation of a mobile number using the above algorithm:

Some examples of checking the validation of a mobile number are given below -

Example 1:

Here we give an example of checking the validation of a mobile number in Python. We import re module for this program. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a Mobile number :7894561230
This is a valid mobile number

Example 2:

Here we give another example of checking the validation of a mobile number in Python. We import re module for this program. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

This is an invalid mobile number

Example 3:

Here we give another example of checking the validation of a mobile number in Python. We import re module for this program. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter the phone number: 2894561230
This is an invalid mobile number

Example 4:

Here we give another example of checking the validation of a mobile number in Python using the '[6-9][0-9]{9}' pattern. We import re module for this program. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a Mobile number :2894561230
This is not a valid mobile number

Algorithm 2 of checking validation of mobile number:

Here we share another algorithm for checking the validation of mobile numbers. We have done it using Python programming language. In Python, there is various kind of modules. 're' module is one of them. This module is used for matching the pattern. The steps for checking the validation of the mobile number are given below -

  1. Firstly, import the 're' module in Python.
  2. Now we must create a function in the re module to check the validation of the mobile number. The function name is full_valid.
  3. Then we must compile a regular expression; its pattern is r'^\d{10}$'. We used the re.compile() method to check a valid 10 digit number.
  4. After that, we will use the bool function. It returns true if the number is valid or the string is matched. Otherwise, it returns false.
  5. Then test all the given inputs.
  6. If the full_valid function passes the number, then it prints the message that the number is validated or not.

Now we give an example using the above algorithm to check list of numbers is valid or not.

Example:

Here we give an example of checking the validation of a mobile number in Python using the above algorithm. We import re module for this program and create a function. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

7894561230 is a valid mobile number
28945612301 is not a valid mobile number
0894561230 is a valid mobile number
9894561230 is a valid mobile number
78945h1230 is not a valid mobile number
78@4561230 is not a valid mobile number

Algorithm 3 of checking validation of mobile number using numpy:

Here we share another algorithm for checking the validation of mobile numbers using numpy. The steps for checking the validation of the mobile number are given below -

  1. First, import Python's 'numpy' and 're' modules.
  2. Then define a function full_Valid which takes 'a' as an input. 'a' is a string value.
  3. After that, they defined a regular expression pattern that must be started between 6 to 9. Any number and the rest digits of the valid mobile number must be starts between 0 to 9.
  4. If a is matched with the regular pattern, it returns true; otherwise, it returns false.
  5. Then we define a mobile number into the string variable 'a'.
  6. After that, call the function full_Valid with variable a.
  7. If the full_Valid function returns true, then print that number is valid; otherwise, the number is not valid.

Example:

Here we give an example of checking the validation of a mobile number in numpy using the above algorithm. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

2894561230 It is not a valid mobile number

Conclusion:

In this article, we are discussing how to check 10 digit mobile number validation in Python. Here we have three different approaches to checking whether the number is validated. But the condition of the validated mobile number is always the same: the number must be started between 6 to 9, any number, and the rest digits of the validated mobile number must be started between 0 to 9, any number. And also, there must not be any special character and alphabet.






Latest Courses