Python Forum
How to verify the give number is valid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to verify the give number is valid
#1
Hi,
I am trying to verify the given number is a valid phone number or not.

Conditions:
1. The number may be a 10 digit one : 9898989876
2. The number can start with "zero" : 09898989876
3. The number can be international : +919898989876

I use below program, and when I enter any of the above number my output is showing "Invalid number"

import re
mblNum =input("Please enter the mobile number to validate\n")
m = re.fullmatch('[+91]*[0]*[6-9]\d{9}', mblNum)
if m != None:
    print("valid number")
else:
    print("Invalid number")
Reply
#2
This is a good source for how re works.
https://docs.python.org/3/library/re.html
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Although this was set to solved, I'm going to post the solution I came up with. It's by far not the best as I've not really got into regrex yet. Hope it will help in the future.
#! /usr/bin/python3

import re
while True:
    num = input('Enter phone number: ')

    if len(num) < 10:
        print('Your number is too short')
    elif len(num) > 13:
        print('Your number is too long')
    else:
        match = re.findall(r'\d', num)

        if match:
            print('valid number')
        else:
            print('Invalid number')
        break
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Regular expression you have matches all the three phone numbers

>>> mblNum =input("Please enter the mobile number to validate\n")
Please enter the mobile number to validate
9898989876
>>> m = re.fullmatch('[+91]*[0]*[6-9]\d{9}', mblNum)
>>> if m != None:
...     print("valid number")
... else:
...     print("Invalid number")
...
valid number
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script getting reindexing only valid error cubangt 1 890 Dec-07-2023, 04:06 PM
Last Post: cubangt
  How to verify widening conversion? quazirfan 1 626 Apr-08-2023, 07:04 AM
Last Post: Gribouillis
Question Use function, retry until valid Ashcora 8 1,463 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  Give visit number ( or counter) within the same time frame balthordu 1 938 Jun-27-2022, 09:51 AM
Last Post: Larz60+
  Give a number for Variable quest 2 1,502 Jan-31-2022, 08:35 AM
Last Post: ibreeden
  checking for valid hexadecimal digits Skaperen 3 6,364 Sep-02-2021, 07:22 AM
Last Post: buran
  Limiting valid values for a variable in a class WJSwan 5 3,600 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Verify input as integer and not blank GMCobraz 3 2,122 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  C:\Windows\System32\appwiz.cpl is not a valid Win32 application Marceepoo 8 5,131 Mar-15-2020, 04:46 AM
Last Post: buran
  is a str object a valid iterator? Skaperen 6 5,623 Jan-27-2020, 08:44 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020