SHARE
TWEET

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

a guest Dec 13th, 2011 48 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.         res = compile(pattern).findall(phn)
  14.         if res:
  15.             return "".join(res[0])
  16.     return None
  17.  
RAW Paste Data
Top