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 :: memory usage in python 
Python :: remove leading and lagging spaces dataframe python 
Python :: python count character occurrences 
Python :: play sound python 
Python :: how to drop column where target column is null 
Python :: combine two columns pandas 
Python :: how to print a string in python 
Python :: how to check if number has decimals python 
Python :: python check phone number 
Python :: how to make a loading gif in pyqt5 
Python :: 405 status code django 
Python :: fullscreen cmd with python 
Python :: stop procedure python 
Python :: python dict comprehension 
Python :: any in python 
Python :: python convert two dimensional list to one dimensional 
Python :: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead 
Python :: display image from sql with python 
Python :: rgb color python 
Python :: python split by first match 
Python :: python callable type hint 
Python :: make a condition statement on column pandas 
Python :: How to store the input from the text box in python 
Python :: numpy array from list 
Python :: current url in djago 
Python :: python extract string 
Python :: python for loop with step 
Python :: python convert string to int 
Python :: how to make every letter capital in python 
Python :: discord bot python time delay 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =