Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to check a phone number is valid in python

import phonenumbers
import re

def validate_mobile(value):
    """ Return False if the value does not looks like a mobile telephone number.
    """
    regex1 = re.compile("[@_!#$%^&*()<>?/|}{~:]")
    regex2=re.compile(r'[a-zA-Z]')
    try:
        if "-" in value:
            value=value.replace('-','')
        print(value)
        #print(value.isdigit())

        if (regex1.search(value) == None) and (regex2.search(value) == None):
            my_number = phonenumbers.parse(value)
            rule1 = phonenumbers.is_possible_number(my_number)#re.compile(r"(^[+0-9]{1,3})*([0-9]{10,11}$)")#(r'^(?:+?44)?[07]d{9,13}$')
            rule2=phonenumbers.is_valid_number(my_number)
           # rule3=carrier._is_mobile(number_type(phonenumbers.parse(value)))
            if rule1 == True and rule2 == True:
                print(rule1,rule2)
                return True
            else:
                return False
        else:
            return False
    except:
        return False
Comment

python check phone number

def isPhoneNumber(text):
    if len(text) != 12: 
        return False 
    for i in range(0, 3): 
        if not text[i].isdecimal(): # if the first 3 text is not decimal string
            return False  
    if text[3] != '-': 
        return False 
    for i in range(4, 7): 
        if not text[i].isdecimal(): 
            return False 
    if text[7] != '-': 
        return False 
    for i in range(8, 12):
        if not text[i].isdecimal(): 
            return False 
    return True 

message = 'Call me at 000-000-0000 tmmr, and 911 is police.' 

for i in range(len(message)):
    chunk = message[i:i+12] 
    if isPhoneNumber(chunk): 
        print('Phone number found: ' + chunk) 
print('Done')
Comment

PREVIOUS NEXT
Code Example
Python :: concatenate two tensors pytorch 
Python :: use of kwargs and args in python classes 
Python :: unsigned int python 
Python :: insert single value in dataframe using index 
Python :: end in print python 
Python :: list comprehension python if else 
Python :: python compiler to exe 
Python :: dfs in python 
Python :: pandas groupby values in list 
Python :: remove unnamed columns pandas 
Python :: numpy get diagonal matrix from matrix 
Python :: django insert template in another template 
Python :: - inf in python 
Python :: how to append panda columns using loop 
Python :: file methods in python 
Python :: concatenating datfra,esin pandas 
Python :: new line in python 
Python :: remove multiple elements from a list in python 
Python :: pandas round floats 
Python :: python timeout exception 
Python :: python try and except 
Python :: create a virtualenv python3 
Python :: current url in djago 
Python :: create new python environment check 
Python :: import path in django 
Python :: how to run shell command ctrl + c in python script 
Python :: how to find the path of a python module 
Python :: hashmap python 
Python :: socket always listen in thread python 
Python :: write list to csv python 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =