Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

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

By: a guest on Dec 13th, 2011  |  syntax: Python  |  size: 0.40 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.