SHARE
TWEET

http://programmingpraxis.com/2011/12/13/validating-telephone

a guest Dec 13th, 2011 307 Never
  1. from re import compile
  2.  
  3. VALID_PHONE_PATTERNS = [
  4.     "\d{10}",
  5.     "\d{3}\-\d{3}\-\d{4}",
  6.     "\d{3}\.\d{3}\.\d{4}",
  7.     "\(\d{3}\)\s?\d{3}\-\d{4}",
  8.     "\d{3}\-\d{4}"]
  9.    
  10. def validate_phone_number(phn):
  11.    
  12.     for pattern in VALID_PHONE_PATTERNS:
  13.         myre = compile(pattern)
  14.         if myre.match(phn):
  15.             return True
  16.     return False
  17.  
RAW Paste Data
Top