{"id":26703,"date":"2022-04-01T00:28:35","date_gmt":"2022-03-31T18:58:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=26703"},"modified":"2022-04-01T00:28:35","modified_gmt":"2022-03-31T18:58:35","slug":"mobile-number-validation-using-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/mobile-number-validation-using-python\/","title":{"rendered":"Mobile Number validation Using Python"},"content":{"rendered":"
If you’re working on a Python project and want to add a mobile number validation feature, you should follow some validation criteria.<\/p>\n
Mobile Number Validation Criteria:<\/strong><\/p>\n All of the criteria listed above must be met in order to validate a mobile number.<\/p>\n NOTE:<\/strong> Here, we used the Indian mobile number validation requirements. You can easily edit the code to fit your country’s criteria.<\/p>\n We can Validate a mobile number using two methods:<\/p>\n Below are the ways to validate the given mobile number using python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If and only if the full string matches the pattern, it returns a match object; otherwise, it returns None. Mobile Number Validation Criteria:<\/strong><\/p>\n Example Pattern:<\/strong><\/p>\n ‘[6-9][0-9]{9}’:<\/strong> It means the starting should be between 6 to 9 and the next nine digits can be any number between 0 and 9.<\/p>\n ‘[6-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]’:<\/strong> This pattern works in the same way as the above one.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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\u00a0digits. The mobile number should only begin with 7, 8, or 9 digits. There should be no characters or strings in the mobile …<\/p>\n\n
\n
Mobile Number validation Using Python<\/h2>\n
\n
Method #1: Using If-Else conditional statements<\/h3>\n
\n
# Give some random 10 digit mobile number as user input using the input() \r\n# function and store it in a variable.\r\ngvn_mobilenumb = input(\"Enter some random 10 digit mobile number = \")\r\n\r\n# Check if the length of the given mobile number is greater than or less than 10\r\n# using the if conditional statement\r\nif len(gvn_mobilenumb) > 10 or len(gvn_mobilenumb) < 10:\r\n # If it is true then print \"Invalid Mobile number. Mobile number must have 10 digits\"\r\n print(\"Invalid Mobile number. Mobile number must have 10 digits\")\r\nelse:\r\n # Else Check if first digit of the given mobile number is 7 or 8 or 9 using the if conditional statement\r\n if gvn_mobilenumb[0] == '7' or gvn_mobilenumb[0] == '8' or gvn_mobilenumb[0] == '9':\r\n # Handling the errors using the try-except blocks\r\n try:\r\n # If it is true, then convert the given mobile number to an Integer using the\r\n # int() function and store it in the same variable.\r\n gvn_mobilenumb = int(gvn_mobilenumb)\r\n # Print the given mobile number is a valid number\r\n print(\"The given mobile number is a Valid Number\")\r\n except:\r\n # Print \"Invalid Mobile number. Mobile number must not contain any characters\"\r\n print('Invalid Mobile number. Mobile number must not contain any characters')\r\n else:\r\n # Else print \"Invalid Mobile number. Mobile number must begin with 7 or 8 or 9\"\r\n print(\"Invalid Mobile number. Mobile number must begin with 7 or 8 or 9\")<\/pre>\n
Enter some random 10 digit mobile number = 9989593198\r\nThe given mobile number is a Valid Number<\/pre>\n
Enter some random 10 digit mobile number = 4567890182\r\nInvalid Mobile number. Mobile number must begin with 7 or 8 or 9<\/pre>\n
Enter some random 10 digit mobile number = 9989583v99\r\nInvalid Mobile number. Mobile number must not contain any characters<\/pre>\n
Method #2: Using Regex Module<\/h3>\n
\nWe can determine whether or not the return value is a valid\u00a0number based on the return value.<\/p>\n\n
\n
# Import re(regex) module using the import keyword\r\nimport re\r\n# Give some random mobile number as user input using the input() \r\n# function and store it in a variable.\r\ngvn_mobilenumb = input(\"Enter some random mobile number = \")\r\n# Pass the Pattern, given mobile number as arguments to the fullmatch() function to \r\n# check if the entire mobile number matches the pattern else it returns None.\r\n# Store it in a variable\r\n# Here the pattern represents that the starting number should be between 6-9\r\n# and the next nine digits can be any number between 0 and 9.\r\nrslt = re.fullmatch('[6-9][0-9]{9}',gvn_mobilenumb) \r\n# Check if the above result is not Equal to None Using the if conditional statement\r\nif rslt!=None: \r\n # If it is true, i.e the output is not None then print \"Valid Number\"\r\n print('The given mobile number is a Valid Number')\r\nelse:\r\n # Else print \"Invalid Number\"\r\n print('The given mobile number is Invalid')<\/pre>\n
Enter some random mobile number = 9494580181\r\nThe given mobile number is a Valid Number<\/pre>\n","protected":false},"excerpt":{"rendered":"